Php 避免重定向到同一页面后重新提交表单
我在网上搜索了很多,但我还是很困惑。我有一个表格,我提交并重定向到它自己。然后,它将变量回显到屏幕上。但是,在用户点击刷新按钮后,如何防止表单重新提交??这是可以实现的吗?如何实现???这是密码Php 避免重定向到同一页面后重新提交表单,php,forms,post,Php,Forms,Post,我在网上搜索了很多,但我还是很困惑。我有一个表格,我提交并重定向到它自己。然后,它将变量回显到屏幕上。但是,在用户点击刷新按钮后,如何防止表单重新提交??这是可以实现的吗?如何实现???这是密码 <!DOCTYPE HTML> <html> <head> </head> <body> <?php // de
<!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>
PHP表单验证示例
简单地说,若存在post值,则不显示submit按钮或删除submit按钮的行为,如下所示
<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>
<?php if(!isset($_POST["name"])) { ?>
<input type="submit" name="submit" value="Submit">
<?php } ?>
</form>
您应该停止这种奇怪的习惯,让表单提交到生成表单的脚本。我一直不明白为什么会这样做。它引起了很多问题。您有一个生成视图、表单的操作,还有一个处理表单的操作。所以有两个独立的脚本。简单、高效和透明。即使我停止提交相同的脚本,表单也会在页面刷新时继续重新提交。你能告诉我怎么停止吗??提交错误数据时,我应该如何向用户显示错误消息???