如何从PHP表单生成CSV文件?

时间:2017-11-13 10:41:08

标签: php html forms csv

意识到这已被问过,但我找到的解决方案并没有帮助。

目前,当用户在我的网站上填写表格以下载宣传册时,我会收到一封电子邮件。我想有这些细节; CSV文件中的姓名,电话,电子邮件和城市商店,然后我可以手动添加到Mailchimp或其他网站。

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>    

非常感谢任何有助于将用户信息导出为CSV文件的帮助。

谢谢,

3 个答案:

答案 0 :(得分:0)

要回答您的问题,您可以轻松地将阵列数据结构转换为CSV文件。这里有人问过:Export to CSV via PHP

在这种情况下,您可以使用POST阵列(尽管您应该先进行一些检查以避免有害的代码注入)。

然而,mailchimp还有一个API,可让您直接添加这些详细信息。如果您之间不需要任何手动步骤,则可以使用此API将您的用户直接添加到邮件列表:http://developer.mailchimp.com/documentation/mailchimp/guides/manage-subscribers-with-the-mailchimp-api/

答案 1 :(得分:0)

将数据放入数组并使用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);

答案 2 :(得分: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>