最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - unset($_GET['someVariable']) not working - Stack Overflow

programmeradmin0浏览0评论

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

 // 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:

 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:

  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.

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.

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'

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

 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.

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?

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

 <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> 

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

 // 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:

 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:

  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.

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.

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'

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

 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.

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?

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

 <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> 
Share Improve this question edited Sep 8, 2013 at 19:54 CFHcoder asked Sep 8, 2013 at 19:39 CFHcoderCFHcoder 4493 gold badges8 silver badges25 bronze badges 4
  • Please upload the code for your form. – Tim Bodeit Commented Sep 8, 2013 at 19:44
  • is there any specific reason you're trying to use GET and POST simultaneously? – James Commented Sep 8, 2013 at 19:44
  • @Tim I edited my code above to show the form, it was big so I shrank it for brevity, the form works fine so any error above is a goof from shrinking it for this question. – CFHcoder Commented Sep 8, 2013 at 19:55
  • Ok ... see my answer. Changing the action of your form should do the trick. – Tim Bodeit Commented Sep 8, 2013 at 19:56
Add a ment  | 

3 Answers 3

Reset to default 4

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

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=someValue at the end of the address.

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.

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

Try setting:

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

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

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

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

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

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";
发布评论

评论列表(0)

  1. 暂无评论