0

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,

C.Pan
  • 1
  • 2

3 Answers3

0

To answer your question you can easily transform an array data structure into a CSV file. This has been asked here: Export to CSV via PHP

In this case you could use your POST array (although you should do some checking first to avoid harmful injections of code).

However mailchimp also has an API that lets you add those details directly. If you do not need any manual steps in between you could add your users directly to the mailing list using this API: http://developer.mailchimp.com/documentation/mailchimp/guides/manage-subscribers-with-the-mailchimp-api/

Gegenwind
  • 1,388
  • 1
  • 17
  • 27
0

Put your data into an array and use fputcsv.

$name=$_POST['fname'];
$email=$_POST['email_from'];
$phone=$_POST['phone'];
$city=$_POST['city'];
$content=$_POST['comments'];
$dataRow = [
    'first_name' => $name,
    'email' => $email,
    'phone' => $phone,
    'city' => $city,
    'comments' => $comments,
];
// You need to decide where you want the CSV file stored and since you are receiving lots of them you might want to use a timestamp in the filename. If you want the CSV file as part of the email you will need to attach it to the mail() message.
$csvFile = 'some/file/path/csvfile.csv';
$fp = fopen($csvFile, 'w');
fputcsv($fp, $dataRow);
fclose($fp);
davidethell
  • 11,708
  • 6
  • 43
  • 63
  • When I tried to implement this into the PHP code, the submit button would open the new tab but the brochure would not appear. – C.Pan Nov 13 '17 at 15:28
  • Without seeing your updated code it's hard to say what it would be. – davidethell Nov 13 '17 at 21:27
  • I have update the original post with your code. I entered the code in the php before the 'if' command. – C.Pan Nov 15 '17 at 11:20
  • Are you initializing the `$someCsvFile` variable with the path to your filename where you want to save the CSV? – davidethell Nov 15 '17 at 11:28
  • I have used header outputs to show where the CSV file is. Please see edit above, however this still doesn't save the information in the file or open the brochure pdf. in a new tab. It seems the two pieces of code i am using are conflicting in some way?? – C.Pan Nov 20 '17 at 10:07
  • @C.Pan you don't want to output CSV headers. All you want to do is save the CSV file somewhere. It is unclear from your question what you want the output to be. Do you want the PDF to be the output to the user? Do you want an HTML results page to be the output? If you want the PDF brochure to be the output then the header output should still be 'application/pdf'. You don't need the CSV header since you are only saving that file. Please see my updated answer. – davidethell Nov 20 '17 at 10:13
0
 <form action='' method='post'>
    <p><label>Name</label><br><input type='text' name='name' value=''></p> 
   <p><label>Email</label><br><input type='text' name='email' value=''></p> 
  <p><input type='submit' name='submit' value='Submit'></p> 
</form>
  • What does this code do? Why does this cold solve the issue? Please Edit and explain your answer. – Martin Mar 03 '20 at 13:16