如何使用php将html5表单中的数据存储到txt文件

如何使用php将html5表单中的数据存储到txt文件,php,Php,我试图使用这个HTML表单从最终用户那里收集信息,并使用PHP将其保存到文本文件中。我正在寻找各种方法来实现这一点,假设文本文件不存在,并检查和追加文件是否存在。有人有什么建议吗 Html格式: <form id="newuser" method="post" action="newUser.php"> <fieldset> <label for="name">Name</label> <input type="te

我试图使用这个HTML表单从最终用户那里收集信息,并使用PHP将其保存到文本文件中。我正在寻找各种方法来实现这一点,假设文本文件不存在,并检查和追加文件是否存在。有人有什么建议吗

Html格式:

<form id="newuser" method="post" action="newUser.php"> 
<fieldset>
    <label for="name">Name</label>
        <input type="text" name="name" placeholder="Full Name">
    <label for="email">E-mail</label>
        <input type="email" name="email" placeholder="hello@world.com">
    <label for="birthday">Birthday</label>                         
        <input type="date" name="birthday" min="1929-12-31">
    <label for="phone">Phone</label> 
        <input type="tel" name="phone" placeholder="ex. (555) 555-5555">        
    <label for="message">Question/Comment</label> 
        <textarea name="message"></textarea>
    <label>Check this box if you agree to the website 
      <a href="terms.php">terms</a> 
        <input type="checkbox" name="agreetoterms" value="Agree"> </label>
        <input type="submit" name="submit" id="submit" value="Join Now" />
  </fieldset> </form>

我真的不知道还需要添加什么,所以希望这能为您解决将用户提交的表单记录到文本文件中的问题提供更多指导。除了检查,您只需执行以下操作

<?php
    /* newuser.php */
    if( $_SERVER['REQUEST_METHOD']=='POST' ){

        /* 
            A path to where you wish to store the file, preferably outside of the document root.
            The example path below is within the document root because there is no way of 
            knowing your directory structure.
        */
        $saveto=$_SERVER['DOCUMENT_ROOT'] . '/folder/file.txt';


        if( !empty( $_POST ) ){
            /* 

                Add the POSTed form contents to your file.
                ------------------------------------------

                >   The return of `file_put_contents` is the number of bytes written to the designated file.
                >   `print_r( $var, true )` makes the output suitable for writing to file.
                >   `FILE_APPEND` & `FILE_TEXT` are two of the possible options `file_put_contents` accepts and are suitable for your question

            */
            $bytes = @file_put_contents( $saveto, print_r( $_POST, true ), FILE_APPEND | FILE_TEXT );


            /* Process the form data however you would normally */
        }


    }
?>

在newuser.php-file\u put\u contents'c:/temp/form.txt中,print\r$\u POST,true,file\u APPEND??请阅读这里的问题。你应该展示你已经尝试过的东西。我们将帮助您修复代码,但我们不是一个代码编写服务。@RamRaider您能否进一步详细说明我为什么在newser.php中进行此更改?我不太明白那行代码在做什么。这就是输入不显示的原因吗?如果是这样,它不应该指向storeFormData.txt吗?