I have been trying to make it work with either fwrite or fputcsv but i always get the errors pointing to parameters. I am newbie in php but I try to experiment and see what it does. but so far it doesn’t either work because of parameters or not writing to the file (nothing on csv file).
I have one big problem. How to have text appear in columns (name, email and comments) while writing all data into three columns?
$csvData = array($name,$email,$comment);
foreach($csvData as $key => $val)
{
$strVal = $val;
}
print $strVal;
$fileHandle="";
$filename='I-have-question.csv';
if(!file_exists($filename)){
$fileHandle = fopen($filename, "a+");
}
fwrite($fileHandle, $strVal);
fclose($fileHandle);
insight/guide would be appreciated. thanks
EDIT
$name=$_POST['contactName'];
$email=$_POST['email'];
$comment=$_POST['comment'];
$csvData = array($name,$email,$comment);
$csvCol = array ('name', 'email', 'comment');
$strVal='';
foreach($csvData as $key => $val)
{
$strVal .= $val;
}
print $strVal;
$fileHandle="";
$filename='I-have-question.csv';
$fileHandle = fopen($filename, "a+");
fputcsv($filehandle,$csvData);
fclose($fileHandle);
Now error says: Warning: fputcsv() expects parameter 1 to be resource, null. I changed according to two inputs. But it doesnt go well.
EDIT #2
Thank you, @Baba and @Quasdunk for the inputs. The only question left is how to bring in the array for $csvCol to be appended to the first row for each column so that the csvData can be appended/written after that. Only once for csvcol. how do I go about it?
EDIT #3
Thank you very much for the help. I investigated on downloading csv from the browser after the user click on the link – is it possible to do that in that same code? or I hardcode the link to the csv? Just wonder. I saw somewhere about downloadable csv where I click submit button and then the file pop up to be saved – thats not what I want but I only need the csv file to be downloaded from the server via the link.
EDIT #4
Thank you, @Baba and @Quasdunk for the inputs again.
if(isset($_POST[$interest]))
{
foreach ($interest as $interests)
{
$interest = $interests . " ,";
}
}
then in csv, it showed “array”, not values of checkboxes, i.e. 1, 2,3,4
$csvData = array($name,$email,$comment,array($interest));
Where have I gone wrong with this?
EDIT #5
Thank you very much, @Baba. I have last question
Do you know by chance how to pass the variable to mail()? For example, after csv is made/written, then mail() should send all the data to user/admin.
So far I got it working but the interest still doesn’t show the selected checkboxes – instead print “Array” similar to csv.
$interest=$_POST[‘interests’];
this is above the forceheader function.
$emailTo = get_option('tz_email');
if (!isset($emailTo) || ($emailTo == '') ){
$emailTo = get_option('admin_email');
}
$subject = 'I Have A Question to Ask from '.$name;
$thanksubject = 'Thank you for the Form Submission';
$body = "Name: $name \n\nEmail: $email \n\nInterest $interest \n\nComments: $comments";
$userbody = "";
$headers = 'From: '.$name.' ' . "\r\n" . 'Reply-To: ' . $emailTo;
//$headers .= 'Bcc: email2@example.com' . "\r\n";
//$headers .= 'Bcc: email3@example.com' . "\r\n";
wp_mail($emailTo, $subject, $body, $headers);
wp_mail($email,$thanksubject, $body, $headers);
$emailSent = true;
This is after writing to csv. it would be sent if there is no error such as blank fields. $interest only register one array, for example if I select all checkboxes, it would show all in email. So far I tested and it only show one last checkbox. Do you know where I went wrong?
UPDATED
I have figured it out and managed to write the checkboxes’ values to csv and emails as well.
This would work , its accepts array and converts it to CSV automatically
A new concept to always force header information even if it does not exist before
==== Interest =====
Thanks
🙂