我有一个用 HTML 编写的表单和一个 PHP 文件,该文件应该能够将输入生成到同一目录中的文本文件中。我之前用一个不同的简单 HTML 文件对其进行了测试,它可以工作,现在我试图将它应用到一个更大的 HTML 文件,但不知何故它不会工作,我找不到它有什么问题。
反馈.html:
<html>
<head>
<script>
function myFunction()
{
alert("Your feedback has been sent! Thank you");
}
</script>
</head>
<body>
<div class="Linksediv">
<h3>Feedback</h3>
<p>Please enter your name and give us some feedback</p>
<form action="form1.php" method="post">
Name<input id="Email" size="30" type="text" name="field1"/><br/>
<textarea id="pass" type="text" name="field2" rows="4" cols="24">Please give us some feedback
</textarea>
<br/>
<input type="button" name="submit" value="submit" id="buttonOk" onclick="myFunction()" value="Show alert box" />
</form>
</div>
</body>
</html>
Form1.php
<?php
if(isset($_POST['field1']) && isset($_POST['field2'])) { //Check if fields are there in HTML file
$data = $_POST['field1'] . ' - ' . $_POST['field2'] . "\n"; //output
$ret = file_put_contents('data1.txt', $data, FILE_APPEND | LOCK_EX); //file dir for data1.txt
if($ret === false) { //check if anything was written down
die('There was an error writing this file');
}
else {
echo "$ret bytes written to file"."\n";
}
}
?>
如果我在我的表单上按提交,那么我只会收到一个带有警报的弹出窗口,并且不会写入文本文件