我的网页“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
}
提交表单后,我重新输入相同的“mywebpage.php”来处理表单的 POST:
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 处理程序中添加了一个“未设置”调用:
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>