Tuesday, 5 July 2011

GET and POST superglobal arrays

They are basically means to pass data from one page to another or within the page itself, for eg. from
an HTML form to a PHP script.
Basic syntax,
$_POST['key'];
$_GET['key'];

The GET method passes variables as a part of the URL. For example when a user is redirected from
a page http://www.google.ca during a search, you might have noticed a ? followed by some data.
These are the variables passed from the previous page.

This is how you use the get method
<a href="page2.php?var=$var&var2=$var2">Click</a>

These variables will now be stored in the GET superglobal array.
print_r($_GET) will return the array.

Now, in page2.php file, you can GET $var and $var2
$a=$_GET['var'];
Now a will contain the value of $var from page1.

No comments:

Post a Comment