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条)

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名