I've got a PHP routine that processes a form and outputs the values to a CSV file. I'm using array_keys()
to create the header row (skipped if there is one). Everything works perfectly except the final header term is "submit" because, of course, my form includes a Submit button. So the data ends up looking like this:
name,email,cell,dob,study,submit
"Temp One",[email protected],646-325-1111,1995-03-31,8,Submit
"Temp Two",[email protected],646-325-2222,1995-03-31,4,Submit
How do I omit the submit button both from the header and the data?
Here's my code:
if(isset($_POST['submit'])) {
$data = array_values($_POST); // get only values
$headers = array_keys($_POST); // keys are headers
if( $fp = fopen('data.csv','a+')) {
$line = fgets($fp);
if(!$line == $headers) {
fputcsv($fp, $headers);
fputcsv($fp, $data);
}
else
{
fputcsv($fp, $data);
}
fclose($fp);
header('Location: thanks.php');
}
}