如何在 php 中将电子邮件从表单写入 csv


How to write emails from form to csv in php?

嘿,我正在内置的 php Web 服务器上测试我的表单到 csv 程序,但我无法完全让它工作。 我正在尝试将电子邮件提交从表单写入 csv 文件,并将每封电子邮件作为新行。

这是我的表格:

  <form id="form" action="welcome.php" method="post">
    <div class="form-group">
    <label for="exampleInputEmail1">Email address</label>
    <input type="email" name="email" class="form-control" id="exampleInputEmail1" placeholder="Email">
  </div>
  <button type="submit" id="submit-btn" class="btn btn-primary">Submit</button>
  </form>

这是我的欢迎.php文件:

<?php
$email = $_POST["email"];
$file = fopen("list.csv", "w");
fputcsv($file, array($email));
fclose($file);

现在,当我单击提交时,没有任何反应。 文本保留在输入字段中,而我的列表中没有任何内容.csv

实际上,fputcsv()方法将数组作为第二个参数,因此您应该尝试

<?php
$email = $_POST["email"];
$file = fopen("list.csv", "w");
echo fputcsv($file, array($email));
fclose($file);
?>

您可以从这里看到fputcsv方法的原型

而不是button type="submit"使用input type="submit"