javascript unset($_GET['someVariable']) 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18687873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 12:53:06  来源:igfitidea点击:

unset($_GET['someVariable']) not working

phpjavascriptforms

提问by CFHcoder

My web page "mywebpage.php" is arrived at by way of a 'GET' from "someotherpage.php":

我的网页“mywebpage.php”是通过来自“someotherpage.php”的“GET”到达的:

 // In someotherpage.php -- go to mywebpage.php and display the form:
  window.location = "mywebpage.php?someVariable=" + theVariable;

I handle this in "mywebpage.php" as follows:

我在“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
  }

When the form is SUBMIT'd, I re-enter the same "mywebpage.php" to handle the POST of 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
  }

The problem is when the user submits the form, the GET handler is still called so the form gets re-initialized.

问题是当用户提交表单时,仍然会调用 GET 处理程序,因此表单会重新初始化。

The reason is, when the user POST's the form, the GET['someVariable'] is still there so the GET handler is re-entered, and then the POST handler code is processed, but by then the GET handler re-initialized the form which confuses the user since they just got done changing the form's values away from the initial settings.

原因是,当用户 POST 的表单时,GET['someVariable'] 仍然存在,因此重新输入 GET 处理程序,然后处理 POST 处理程序代码,但此时 GET 处理程序重新初始化了表单这让用户感到困惑,因为他们刚刚完成将表单的值从初始设置更改为其他值。

In other words, when the form is submitted, the POST array is being correctly filled, but the GET array hangs around for the ride with its old variables still there, including 'someVariable'

换句话说,当提交表单时,POST 数组被正确填充,但 GET 数组仍然存在,其旧变量仍然存在,包括“someVariable”

So I added an 'unset' call in the GET handler:

所以我在 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']));
  }

THIS FAILED TO WORK. When the form is posted, I'm still seeing that the GET handler above is called.

这无法正常工作。当表单发布时,我仍然看到上面的 GET 处理程序被调用。

I need to make sure when the form is submitted, the GET handler is not re-called -- why doesn't the "unset()" code above work?

我需要确保提交表单时,不会重新调用 GET 处理程序——为什么上面的“unset()”代码不起作用?

EDIT: Here is the form, not everything just the important parts (I left out a lot of inputs, several img tags, etc., nothing more):

编辑:这是表格,不是所有的东西只是重要的部分(我遗漏了很多输入,几个 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="[email protected]" />
  <input type="submit" id="theformSubmitButton" name="theformSubmitButton" value="Post Now">
  </form> 

回答by Tim Bodeit

GET Request and the $_GET variable are on two different layers.

GET 请求和 $_GET 变量位于两个不同的层上。

When your user gets directed to mywebpage.php, there is data passed through get. That is ok this far, however that data is still inside your users current URL. You can see that by looking at the address bar. There will be a ?someParameter=someValueat the end of the address.

当您的用户被定向到 mywebpage.php 时,就会通过 get 传递数据。到目前为止还可以,但是该数据仍在您用户的当前 URL 中。您可以通过查看地址栏看到这一点。?someParameter=someValue地址末尾会有一个。

The unset function will only work on your server and only for the one time execution of your script. It will not remove the GET information from the URL in your users browser.

unset 函数仅适用于您的服务器,并且仅适用于一次执行您的脚本。它不会从用户浏览器中的 URL 中删除 GET 信息。

When you submit data through your HTML form, you redirect the user to the same url, which includes the GET data, which is notstill there, but is being resubmitted again.

当您通过 HTML 表单提交数据时,您会将用户重定向到相同的 url,其中包含 GET 数据,该数据不在那里,但会再次重新提交。

Try setting:

尝试设置:

<form action="mywebpage.php" method="post">

This will set a custom target for your html form, which removes the GET information.

这将为您的 html 表单设置自定义目标,从而删除 GET 信息。

回答by Mr. B.

I'd avoid using $_POSTor $_GETdirectly as often as possible. Reason: you're able to filter/modify the requests global. Simple Example:

我会尽可能避免直接使用$_POST$_GET。原因:您可以过滤/修改全局请求。简单示例:

$myPost = $_POST;
$myGET  = $_GET;
unset($myGET["someVariable"]);

Generally: why're you using $_GETand $_POSTat the same time? If you've to do it, you could handle it with if, if elseand else.

一般:你为什么同时使用$_GET$_POST?如果你必须这样做,你可以用if,if else和来处理它else

回答by Alqin

That's because you unset the variable but

那是因为您取消了变量设置,但是

window.location = "mywebpage.php?someVariable=" + theVariable;

is the same.

是一样的。

My guess is that you need to change when the variable is not set to have something like this:

我的猜测是,当变量未设置为具有以下内容时,您需要进行更改:

window.location = "mywebpage.php";