我目前正在开发一些webform。 这个表单应该将收集的数据发送到webserver上的txt文件,但不知怎的,它给了我错误。
形式:
<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/35443812和questions/14998961
通常,我不会在16:00到08:00之间回复
提前谢谢!
答案 0 :(得分:2)
“文件写入错误”主要是由托管服务提供商中的权限设置不正确引起的。可能,您必须设置775权限才能写入文件。此链接可能对您有用https://unix.stackexchange.com/questions/35711/giving-php-permission-to-write-to-files-and-folders
关于您的PHP代码。我的建议是使用以下代码改进第1行:
<?php
if ( isset( $_POST['submit'] ) ) {
}
?>
此致 编
答案 1 :(得分:0)
只需输入php $ name = $ _POST [&#39; name&#39;]; $ city = $ _POST [&#39; city&#39;]; $ sex = $ _POST [&#39; sex&#39;]; 。如果输入echo $ name;你可以看到数据