CSV files are simply in the format of each row being on a separate line, and each column being separated by a comma. For example:
row1column1, row1column2, row1column3
row2column1, row2column2, row2coumn3
If your data has commas in it, it has to be in quotes:
"row1, column1", "row1, column2", etc, etc
So your code, when updated for this format, would be:
function quote_encapse($str)
{
return '"'.$str.'"';
}
// Checkbox handling
$field_1_opts = $_POST['field_1'][0]."
". $_POST['field_1'][1]."
". $_POST['field_1'][2];
// Saving $_POST['field_1'] and initializing array
$fileLine = array(0=>$field_1_opts);
foreach($_POST as $name => $value) {
/* This is my lazy way of changing the code,
so I don't have to do a ton of typing
All it does is go through the POSTed variables
and if the name that is given has the word
"field_" in it, it will store it to the CSV file */
if($name == "field_1") {
// Just continue to the next one
// we already saved this one
continue;
}
if(strpos($name, "field_") > -1) {
$fileLine[] = $value;
}
}
// Adding your time stamp
$fileLine[] = $dtstamp;
// This will put quotes around our values
$fileLine = array_map("quote_encapse", $fileLine);
// Turning fileLine from an array into a string
// separating each element with a comma
$fileLine = implode(",", $fileLine);
$filename = 'kitrequest.csv';
if (is_writable($filename)) {
if (!$handle = fopen($filename, 'a'))
{
echo "Could not open the order database. Please contact the admin.";
exit;
}
if (fwrite($handle, $fileLine) === FALSE) {
// Security issue: do not expose writable filenames
// to the public, even if there's an error!
echo "Could not save your order. Please contact the admin.";
exit;
}
fclose($handle);
} else {
echo "The order database is not available right now. Please contact the admin.";
}
include("confirm.html");
?>
I have not tested this code, so I can't be sure it works, so let me know if you have any problems. Don't test it on a live server.
Also, this is not an ideal way to do this. You should use a database. Databases can be scary for beginners, but PHP is nearly useless without them, and they aren't all that hard. PM me if you need a link to a good tutorial for beginners, it is really worth your while.