この投稿の理由は、フォームが更新されたときにほとんどのブラウザで発生する「フォームの再送信エラー」を回避しようとしているためです。
次の例を見てください。
ユーザーは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;
}
}
?>
だから、物事を要約すると:
フォームの再送信の問題を回避したいので、コードでヘッダーリダイレクトを使用しています