title: How to Generate CSV File from PHP Form?
body: <p>Aware this has been asked before but the solutions I found didn't help. </p>
  
  <p>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. </p>
  
  <p><strong>PHP:</strong></p>
  
  <pre><code>&lt;?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 = '&lt;html&gt;&lt;body&gt;';
    $message .= '&lt;table rules="all"  cellpadding="10"&gt;';
    $message .= "&lt;tr style='background: #eee;'&gt;&lt;td&gt;&lt;strong&gt;Name:&lt;/strong&gt; &lt;/td&gt;&lt;td&gt;" . $name . "&lt;/td&gt;&lt;/tr&gt;";
    $message .= "&lt;tr style='background: #eee;' &gt;&lt;td&gt;&lt;strong&gt;Email:&lt;/strong&gt; &lt;/td&gt;&lt;td&gt;" . $email . "&lt;/td&gt;&lt;/tr&gt;";
    $message .= "&lt;tr style='background: #eee;'&gt;&lt;td&gt;&lt;strong&gt;Phone:&lt;/strong&gt; &lt;/td&gt;&lt;td&gt;" . $phone . "&lt;/td&gt;&lt;/tr&gt;";
    $message .= "&lt;tr style='background: #eee;'&gt;&lt;td&gt;&lt;strong&gt;City:&lt;/strong&gt; &lt;/td&gt;&lt;td&gt;" . $city . "&lt;/td&gt;&lt;/tr&gt;";
    $message .= "&lt;tr style='background: #eee;'&gt;&lt;td&gt;&lt;strong&gt;Message:&lt;/strong&gt; &lt;/td&gt;&lt;td&gt;" . $content . "&lt;/td&gt;&lt;/tr&gt;";
    $message .= "&lt;/table&gt;";
    $message .= "&lt;/body&gt;&lt;/html&gt;";
  
  
  
              //Creates the csv file. 
              header('Content-Type: text/csv; charset=utf-8');
              header('Content-Disposition: attachment; filename=downloadcsv.csv');
  
              $dataRow = [
              'first_name' =&gt; $name,
              'email' =&gt; $email,
              'phone' =&gt; $phone,
              'city' =&gt; $city,
              'comments' =&gt; $comments,
              ];
              $fp = fopen($downloadcsv, 'w');
              fputcsv($fp, $dataRow);
              fclose($fp);
  
  
  
        if (mail($to, $subject, $message, $headers))
        {
        header('brochure.pdf');
        }
  ?&gt;
  </code></pre>
  
  <p><strong>HTML:</strong></p>
  
  <pre><code>&lt;form name="myForm" action="amail_brochure.php" method="post" onsubmit="return validateForm()" class="sidebar-form" accept-charset="ISO-8859-1"&gt;
    &lt;div class="main-form"&gt;
      &lt;div&gt;
        &lt;input type="text" class="form-control" name="fname" placeholder="Vollständiger Name" required="required"/&gt;         
      &lt;/div&gt;
      &lt;div&gt;
        &lt;input type="email" class="form-control" name="email_from" id="email_from" placeholder="Email" required="required" /&gt;
      &lt;/div&gt;
      &lt;div&gt;
        &lt;input type="text" class="form-control" name="phone" placeholder="Telefon" required="required"/&gt;        
      &lt;/div&gt;
      &lt;div&gt;
        &lt;input type="text" class="form-control" name="city" placeholder="Deine Stadt" required="required" /&gt;
      &lt;/div&gt;
      &lt;textarea name="comments" id="" rows="6" class="form-control" placeholder="Comments:"&gt;&lt;/textarea&gt;
      &lt;div class="button-container" style="display: inline-block;"&gt;
        &lt;button class="btn cta-button pull-right form-submit" type="submit" formtarget="_blank"&gt;Download&lt;/button&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/form&gt;    
  </code></pre>
  
  <p>Any help to implement exporting the users information into a CSV file would be greatly appreciated. </p>
  
  <p>With thanks,</p>