I’m trying to process my Form submit values to a .csv file using the fputcsv function. I’m not getting any errors and no Form values have been saved to the data.csv file. Can anyone see a problem as to what i’m doing wrong?
Form
<label for="location">How many Locations?</label>
<input id="location" name="loc" /><br />
<select id="when" name="day">
<option value="Sunday">Sunday</option>
<option value="Monday">Monday</option>
<option value="Tuesday">Tuesday</option>
<option value="Wednesday">Wednesday</option>
<option value="Thursday">Thursday</option>
<option value="Friday">Friday</option>
<option value="Saturday">Saturday</option>
</select>
<input type="checkbox" id="osecom" name="osecom" value="Yes" />
<label for="osecom">Online</label>
<input type="checkbox" id="item1yes" name="item1yes" value="Yes" /> Yes
<input type="checkbox" id="item1no" name="item1no" value="No" /> No<br />
<label for="chkitem1">mobile</label>
<input type="checkbox" id="item2yes" name="item2yes" value="Yes" /> Yes
<input type="checkbox" id="item2no" name="item2no" value="No" /> No<br />
<label for="chkitem2">policy? </label>
<input type="submit" title="Submit Form" value="" />
php code
if(isset($_POST['submit'])) {
$data = implode(',', $_POST);
if( $fp = fopen('data.csv', 'a') ){
fputcsv($fp, $data);
}
fclose($fp);
}
Don’t use implode(‘,’, $_POST)!
$_POST is array and fputcsv() expects second param to be array not string!
Better use this code:
Note that by saving only values you will have problems reading the csv if parameters in $_POST switch places so it would be better to save first row with headers:
Edit:
I have noticed that in your form you are using checkboxes where radio buttons would fit better like for field “mobile?” and “policy?”