2017-09-15 167 views
0

我目前正在研究一些网络表单。 这个表格应该将收集的数据发送到web服务器上的txt文件,但不知何故,它给了我错误。通过PHP发送HTML表单数据到网络服务器

形式:

<form action="my_webserver_path_to_script.php" name="form" method="post"> 
    <div> 
     <label for="name">Name</label> 
     <input name="name" type="text" id="name" placeholder="Your Name" required/> 
    </div> 
    <div> 
     <label for="city">City</label> 
     <input name="city" type="text" id="city" placeholder="Where do you live?" required/> 
    </div> 
    <div> 
     <label for="sex">Sex</label> 
     <select name="sex" id="sex" required> 
      <option value="" selected disabled hidden>Your sex</option> 
      <option value="man">Man</option> 
      <option value="woman">Woman</option> 
     </select> 
    </div> 
    <div style="margin-top: 30px; text-align: center;"> 
     <input class="button" type="submit" value="Join Us!"/> 
    </div> 
</form> 

脚本:

<?php 
    if(isset($_POST['name']) && isset($_POST['city']) && isset($_POST['sex'])) { 
     $data = $_POST['name'] . '-' . $_POST['city'] . '-' . $_POST['sex'] . "\n"; 
     $ret = file_put_contents('my_webserver_path_to_data.txt', $data, FILE_APPEND | LOCK_EX); 
     if($ret === false) { 
      die('There was an error writing this file'); 
      } 
      else { 
      echo "$ret bytes written to file"; 
     } 
    } 
    else { 
     die('no post data to process'); 
     } 
?> 

当我填写的所有字段,然后点击提交,跳转到PHP地址,并打印“时出错写这个文件”。
我已阅读:questions/35443812questions/14998961
通常,我不会在16:00和08:00之间作出回应。
在此先感谢!

+0

数据是否写入您的文件?也请确保您的.txt文件路径正确 –

+1

显示此错误是因为您的if条件失败:if($ ret === false)因此请检查文件是否可写 –

+0

不可以,数据不会写入文件。路径是100%正确的。 – Rahtid

回答

0

只需输入php $ name = $ _POST ['name']; $ city = $ _POST ['city']; $ sex = $ _POST ['sex']; 。如果你输入echo $ name;您可以看到数据

2

“文件写入错误”主要是由托管提供程序中的权限设置不正确引起的。可能,您必须设置775权限才能写入文件。此链接可能对您有用https://unix.stackexchange.com/questions/35711/giving-php-permission-to-write-to-files-and-folders

关于您的PHP代码。我的建议是提高线#1与后续代码:

<?php 
 
    if (isset($_POST['submit'])) { 
 
    } 
 
?>

问候, 埃德。

+0

我已经考虑过许可问题,但直到星期一才能检查。你的意思是我应该用发送的行缩写整个脚本? – Rahtid

+0

它*可以发送表单,而不需要他需要的所有输入。明确检查他需要什么更好。虽然你可以使它更清洁一些:if(isset($ _ POST ['name'],$ _POST ['city'],$ _POST ['sex'])){' – ishegg

+0

所有的2个输入,并选择是必要的。我改变了你建议的路线。 – Rahtid

相关问题