我目前正在研究一些网络表单。 这个表格应该将收集的数据发送到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/35443812和questions/14998961
通常,我不会在16:00和08:00之间作出回应。
在此先感谢!
数据是否写入您的文件?也请确保您的.txt文件路径正确 –
显示此错误是因为您的if条件失败:if($ ret === false)因此请检查文件是否可写 –
不可以,数据不会写入文件。路径是100%正确的。 – Rahtid