Retrieving GET or POST Data

Once your visitor has filled out a form and submitted it you will want to display another page. On that page you may want to display information about what they entered on the form. Even if you don't you will probably have some form of processing that needs to capture everything that was entered on the form. The following code will easily retrieve all of the GET and POST data for you and load it into appropriately named PHP variables. The same code will also work to get parameters added to the end of URLs via other methods other than using GET with a form.

$q = explode("&",$_SERVER["QUERY_STRING"]);
foreach ($q as $qi)
{
  if ($qi != "")
  {
    $qa = explode("=",$qi);
    list ($key, $val) = $qa;
    if ($val)
      $$key = urldecode($val);
  }
}
 
reset ($_POST);
while (list ($key, $val) = each ($_POST))
{
  if ($val)
    $$key = $val;
}

Here are some examples of it in use. First a simple link to htmlt59.php?parmone=abc&parmtwo=xyz

Next we have a simple form that uses the GET method to pass the values that you enter to the next page:

Parmone:
 
Parmtwo:
 

The code for this form is as follows:

<div class="centre"><form name="ex" action="htmlt59.php" method="get">
Parmone: <input type="text" name="parmone" value="" size="10" /><br />&nbsp;<br />
Parmtwo: <input type="text" name="parmtwo" value="" size="10" /><br />&nbsp;<br />
<input type="submit" /></form></div>

And finally, the same form using POST instead of GET:

Parmone:
 
Parmtwo:
 

This form is coded exactly the same as the preceding one except using method="post" instead of method="get".

Note that if you are using an older version of PHP that you may need to substitute $HTTP_SERVER_VARS for $_SERVER and $HTTP_POST_VARS for $_POST in order to get the code to work.

Also note that this code effectively bypasses the additional security that is provided by disabling register_globals in PHP. A small alteration to this code can add that protection back in.

 

This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow
Donate