我的 AJAX 代码拒绝将填充到 html 表单中的帖子。而不是实际内容,我的txt文件只是填满了"名字姓氏年龄"一词。我哪里出错了?
(我有一个提交按钮,按下该按钮时,将隐藏div并打开另一个 - 不知道这是否是重要信息)
这是 HTML:
General intro
<form action="action_page.php">
<fieldset>
<legend>Personal information:</legend>
First name:<br>
<input type="text" name="firstname">
<br>
Last name:<br>
<input type="text" name="lastname">
<br>
Age (in numbers; e.g., "47", not "forty-seven"):<br>
<input type="text" name="age">
<br><br>
</fieldset>
</form>
<p><button type=button id='Submit0'>Submit my information</button></p>
这是我action_page.php中的代码
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$age = $_POST['age'];
$out;
$result = file_put_contents("log.txt", PHP_EOL ."firstname" . $firstname . " lastname " . $lastname ."age" . $age, FILE_APPEND);
if ($result !== FALSE) {
$out = [ "message" => "success!", "status" => 200];
}
else {
$out = [ "message" => "error saving file", "status" => 500];
}
echo json_encode($out);
exit();
这对我来说不像Ajax,但是您应该能够通过更改来解决您的问题
<form action="action_page.php">
自
<form action="action_page.php" method="POST">