私の Web ページ "mywebpage.php" は、"someotherpage.php" からの 'GET' によって到達されます。
// In someotherpage.php -- go to mywebpage.php and display the form:
window.location = "mywebpage.php?someVariable=" + theVariable;
これを「mywebpage.php」で次のように処理します。
THIS IS THE 'GET' HANDLER for the form IN "mywebpage.php":
if(isset($_GET['someVariable']))
{
// set up form variables to initial values, then display the form
}
フォームが SUBMIT されたら、フォームの POST を処理するために同じ「mywebpage.php」を再入力します。
THIS IS THE 'POST' HANDLER IN "mywebpage.php"
// okay the form was submitted, handle that here...
if(isset( $_POST['theformSubmitButton']))
{
// handle the form submit
}
問題は、ユーザーがフォームを送信したときに、GET ハンドラーがまだ呼び出されているため、フォームが再初期化されることです。
その理由は、ユーザーがフォームを POST するとき、GET['someVariable'] がまだそこにあるため、GET ハンドラーが再入力され、POST ハンドラー コードが処理されますが、それまでに GET ハンドラーがフォームを再初期化したためです。フォームの値を初期設定から変更したばかりなので、ユーザーを混乱させます。
つまり、フォームが送信されると、POST 配列は正しく入力されますが、GET 配列は、'someVariable' を含む古い変数がまだそこにある状態で、ライドのためにハングアップします。
そこで、GET ハンドラーに「unset」呼び出しを追加しました。
this is the MODIFIED 'GET' HANDLER in "mywebpage.php"
if(isset($_GET['someVariable']))
{
// set up form variables then display the form
// now clear out the 'someVariable' in the GET array so that
// when the user POST's the form, we don't re-enter here
unset($_GET['someVariable']));
}
これは機能しませんでした。フォームが投稿されると、上記の GET ハンドラーが呼び出されていることがわかります。
フォームが送信されたときに、GET ハンドラーが再度呼び出されないようにする必要があります。上記の「unset()」コードが機能しないのはなぜですか?
編集: ここにフォームがありますが、重要な部分だけではありません (多くの入力、いくつかの img タグなどを省略しましたが、それ以上のものはありません):
<form enctype="multipart/form-data" action="#" style="display: inline-block"
method="post" name="myForm" id="myFormId">
<textarea name="InitialText" id="theText" rows="4" cols="68"
style="border: none; border-style: none"></textarea>
<br />
<label style="font-weight: bold; font-style: italic">For more info provide an email addres:</label>
<input type="text" id="emailField" name="emailFieldName" value="you@gmail.com" />
<input type="submit" id="theformSubmitButton" name="theformSubmitButton" value="Post Now">
</form>