This article tells you how to pass variables to a PHP page using the query string, and how to access them from that page.
Introduction
Have you ever seen a URL which looked like "www.example.com/page.php?mode=1&style=red"? Well, this page is being passed variables and their values through the query string, here the variables "mode" and "style" are being passed, with values "1" and "red" respectively. The question mark indicates the start of the query string and the ampersand, &, symbol seperates variable=value assignments. The page is then able to read these variables and react according to them, for example to display them to the user.
Passing a query string to a page
There are two ways to pass a QueryString to a page, the first is to enter it manually, for example:
<a href="page.php?mode=1">Mode 1</a>
The above HTML creates a link to a page passing the variable mode with the value "1".
An alternative, and perhaps more useful, way is to use HTML forms. The main thing to remember with forms is that you need to use the GET method to send information via the query string, for example:
<form method="GET" action="page.php"> Please enter your name: <input type="text" name="username" /> <input type="submit" value="submit" /> </form>
This code displays a simple HTML form with a text box and a submit button. When the user enters their name and presses submit, the information in the name box is passed to a page called "page.php" (specified in the form's action attribute) via the query string. So, if I entered "mooseh" and pressed submit, the form will call page.php?username=mooseh. Remember that "username" was what we entered as the text input's name attribute.
Accessing a query string element in a PHP page
In PHP, all the information passed via the query string is held in the $_GET super global array (prior to PHP version 4.1.0 it was called $HTTP_GET_VARS, but that's now depreciated). To access an item, type $_GET['varName'], where varName is the name of the variable in the query string.
To demonstrate, let's create page.php to process the information from the form above:
<?
if($_GET['username'] == "")
{
// no username entered
echo "You did not enter a name.";
}
else
{
echo "Hello, " . $_GET[’username’];
}
?>
So now, me typing in mooseh and pressing submit will generate the reply "Hello, mooseh".
Listing the contents of $_GET
Should you wish to find out everything that's in the query string, you can do so with the following code which displays the variables next to their values in a table:
<table border="1">
<tr><th>variable</th> <th>value</th></tr>
<?
foreach($_GET as $variable => $value)
{
echo "<tr><td>" . $variable . "</td>";
echo "<td>" . $value . "</td>";
}
?>
</table>
Conclusion
This article showed you how to pass information between pages with the query string, and a use which allows you to add some increased interactivity. There are a lot of other uses, for example selecting which section of a site to display based on a variable in the query string, and processing form data.
wonderfull! thanu, u have explained me a lot.
i followed the instruction , but other than the table i couldnt get the variables to show.
good day! i am trying to know how to pass values from 1 page to another. i got it when i passed textfield but i also want to pass values from my table. isaw ur example but can u make a much clear example?
is there a way to pass an array variable from 1 page to another?
i hope u can help me.
thank you in advance
is there a full tutorial on this subject?
Nice Explanation...
How to pass more than one query string
I had to modify the code to use double quotes if($_GET[”username”] == “”)
Then it worked for me. I am runing PHP 4.4.4
Thanx buddy. I understood very well, & now it is working very clearly...
Thanxx again
Hi,
many thanks for this descriptions, nice to read.
Greetz John
thanks it gives lot help to me
How can i send query string when calling the php file with include (inside another php file) ?
Thx for sharing your approach! Your post helped me allot Im creating a complex product configurator within mage. At the end, I need a “add to cart” function of my created product and this snipet worked like a charm