duanji1902 2014-06-16 23:04
浏览 98
已采纳

提交表单并写入多个表单字段的文件

I have a code of a form that writes the "email" of user into a .txt file in my server.

Here are some things I want to do: Make the form have more than one variable (Like, one line to "name" and another one to "email"), put the form writing in .txt prefixes before the "input" texts, and make the output .txt file have line breaks between the contents of each variable.

Here is my code currently

<?php
if(isset($_POST['submit']))
{
   $email = $_POST['email'];
   $file = fopen("emaillist.txt","a+");
   fwrite($file,$email);
   fclose($file); 
   print_r(error_get_last());
}
?>
<form action= "" method="post" name="form">
   Email:
   <input type="email" name="email">
   <br>
   <br>
   <input type="submit" name="submit" value="submit">
   <br>
</form>

Can you help me? Thank you all.

  • 写回答

2条回答 默认 最新

  • dte66654 2014-06-16 23:24
    关注

    You can add another input to your form. In your code, you have <input type="email" name="email"> for the email address. Just add a <input type="text" name="name"> for the name. The field's name (name="xxx") is the key that you can use in PHP in $_POST['xxx'] to get the information for another line.

    Did I understand your question "How can I put the form writting in .txt prefixes before the "input" texts?" correct that you want to have an output in your file like "Email: x@x.x"?

    To add a line break, you can write " " (if you use a Windows System, use " ") in your strings.

    Instead of fopen(), fwrite() and fclose() you can use file_put_contents() which makes your code a bit easier to read.

    You can try this code:

    <?php
    if(isset($_POST['submit']))
    {
      $name = $_POST['name'];
      $email = $_POST['email'];
      $new_content = "
    Name: " . $name;
      $new_content .= "
    Email: " . $email;
      file_put_contents('emaillist.txt', $new_content, FILE_APPEND);
      print_r(error_get_last());
    }
    ?>
    <form action="" method="post" name="form">
      Name:
      <input type="text" name="name"><br>
      Email:
      <input type="email" name="email">
      <br>
      <br>
      <input type="submit" name="submit" value="submit"><br>
    </form>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义