这篇文章的原因是我试图避免刷新表单时大多数浏览器发出的“表单重新提交错误”。
举个例子:
用户在一个名为signpetition.php的页面上,并显示了一个基本的 HTML 表单:
<div id="sign-errors" style="display: none;">
</div>
<form action="" method="post">
<input type="submit" name="submit_button" value="sign petition" />
</form>
因此,用户单击submit
按钮,现在将读取和验证 POST 数据。
signpetition.php
<?php
if(isset($_POST['submit_button']))
{
// Do some validation
if($errors == true)
{
// What to do here? I want to go back to the last page, but keep the errors. Should I store them in a session variable and then unset them after their use? I feel as though there must be a better solution to this.
$_SESSION['error'] = "the error";
header('Location: '. $_SERVER['REQUEST_URI'] , true, 303);
exit;
}
else
{
$_SESSION['success'] = "your petition was successfully signed!";
header('Location: '. $_SERVER['REQUEST_URI'] , true, 303);
exit;
}
}
?>
所以,总结一下:
我想避免表单重新提交问题,这就是我在代码中使用标头重定向的原因