2016-09-15 47 views
-3

我想使用這個HTML表單從最終用戶收集信息並使用PHP將其保存到文本文件中。我正在尋找各種方式,我可以通過假設文本文件不存在以及如果文件存在時進行檢查和追加來完成此操作。有沒有人有什麼建議?如何使用php將html5格式的數據存儲到txt文件

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="[email protected]"> 
    <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> 
+0

在'newuser.php' - 'file_put_contents( 'C:/temp/form.txt' 的print_r($ _ POST,真),FILE_APPEND);' ?? – RamRaider

+0

請在這裏閱讀關於[問]的問題。你應該顯示你的嘗試。我們將幫助您修復代碼,但我們不是代碼編寫服務。 –

+0

@RamRaider你可以進一步闡述爲什麼我在「newser.php」中做這個改變嗎?我不太明白那行代碼在做什麼。這就是爲什麼輸入不顯示?如果是這樣,它不應該指向「storeFormData.txt」嗎? – Chris

回答

0

我真的不知道有什麼更多的補充讓我們希望這給更多的指導,你記錄用戶的表單提交到一個文本文件來解決問題。除了 「檢查」 你根本就做如下

<?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 */ 
     } 


    } 
?> 
相關問題