Aware this has been asked before but the solutions I found didn't help.
Currently I receive an email when a user completes the form on my website to download the brochure. I would like to have these details; Name, telephone, email and city store in a CSV file, which I can then manually add to Mailchimp or other site.
PHP:
<?php
$name = $_POST['fname'];
$email = $_POST['email_from'];
$phone = $_POST['phone'];
$city = $_POST['city'];
$content = $_POST['comments'];
$to = 'C.PAN@email.com';
$subject = 'Enquiry';
$headers = "From: " . strip_tags($email) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= '<table rules="all" cellpadding="10">';
$message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>" . $name . "</td></tr>";
$message .= "<tr style='background: #eee;' ><td><strong>Email:</strong> </td><td>" . $email . "</td></tr>";
$message .= "<tr style='background: #eee;'><td><strong>Phone:</strong> </td><td>" . $phone . "</td></tr>";
$message .= "<tr style='background: #eee;'><td><strong>City:</strong> </td><td>" . $city . "</td></tr>";
$message .= "<tr style='background: #eee;'><td><strong>Message:</strong> </td><td>" . $content . "</td></tr>";
$message .= "</table>";
$message .= "</body></html>";
//Creates the csv file.
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=downloadcsv.csv');
$dataRow = [
'first_name' => $name,
'email' => $email,
'phone' => $phone,
'city' => $city,
'comments' => $comments,
];
$fp = fopen($downloadcsv, 'w');
fputcsv($fp, $dataRow);
fclose($fp);
if (mail($to, $subject, $message, $headers))
{
header('brochure.pdf');
}
?>
HTML:
<form name="myForm" action="amail_brochure.php" method="post" onsubmit="return validateForm()" class="sidebar-form" accept-charset="ISO-8859-1">
<div class="main-form">
<div>
<input type="text" class="form-control" name="fname" placeholder="Vollständiger Name" required="required"/>
</div>
<div>
<input type="email" class="form-control" name="email_from" id="email_from" placeholder="Email" required="required" />
</div>
<div>
<input type="text" class="form-control" name="phone" placeholder="Telefon" required="required"/>
</div>
<div>
<input type="text" class="form-control" name="city" placeholder="Deine Stadt" required="required" />
</div>
<textarea name="comments" id="" rows="6" class="form-control" placeholder="Comments:"></textarea>
<div class="button-container" style="display: inline-block;">
<button class="btn cta-button pull-right form-submit" type="submit" formtarget="_blank">Download</button>
</div>
</div>
</form>
Any help to implement exporting the users information into a CSV file would be greatly appreciated.
With thanks,