0
我已经在互联网上搜索了所有内容,但我仍然感到困惑。我有一个表格,我提交并重定向到它自己。然后它将变量回显到屏幕上。但是如何在用户点击刷新按钮后阻止表单重新提交?这是可以实现的吗?这里是代码重定向到同一页面后避免表单重新提交php
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<?php
// define variables and set to empty values
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$website = test_input($_POST["website"]);
$comment = test_input($_POST["comment"]);
$gender = test_input($_POST["gender"]);
echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
}
if($_SERVER["REQUEST_METHOD"] == "GET")
{
header('Location: '. $_SERVER['PHP_SELF'] , true, 303);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h2>PHP Form Validation Example</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name">
<br><br>
E-mail: <input type="text" name="email">
<br><br>
Website: <input type="text" name="website">
<br><br>
Comment: <textarea name="comment" rows="5" cols="40"></textarea>
<br><br>
Gender:
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
我是新来这....请帮助。
你应该停止那种奇怪的习惯,有一个表格提交到生成脚本形成。我从来不明白为什么这样做。它会导致很多问题。你有一个动作产生一个视图,表单和处理表单的另一个动作。所以两个单独的脚本。简单,高效和透明。 – arkascha
即使我停止提交到相同的脚本,表单不断重新提交页面刷新。你能请教如何阻止?当提交错误数据时,我应该如何向用户显示错误消息? – user3485417