PHP Get

$_GET is a variable used to gather values from a form.

You may also wish to read the lesson on the $_POST variable

The $_GET variable is sent from the form, with a maximum allowable size of 100 characters.

Let’s create a simple form, which will send the information using the PHP $_GET variable:

<form action=”form.php” method=”get”>
Color: <input type=”text” name=”color” />
Gallons: <input type=”text” name=”gallons” />
<input type=”submit” name=”submit” value=”Send!”>
</form>

Using the above HTML, we would have created a form with two text boxes and a submit button. In the first text box, the user would enter the color of paint they desire. In the second box, they enter the number of gallons they would like.

The last part of the form we have a submit button, which when clicked, will send the data to the file “form.php” to be processed using PHP $_GET.

When the “Send!” button is clicked, the data will be sent to “form.php”, and the URL could look like:

http://www.example.com/form.php?color=Black&gallons=8

Notice that the elements entered in the form become part of the URL. This is why you would never use this method when submitting sensitive data such as a password or credit card number. However, when you want to create a dynamic page that a visitor can bookmark, using $_GET can be very useful. For example, if you have an online store and a user searches for size large shirts, the URL may look like:

http://www.example.com/search.php?item=shirt&size=large

The visitor would have the ability to bookmark this page, and easily revisit your site straight from a page that only displays large shirts.

An array will automatically be made based on the information submitted:

$_GET["color"];
$_GET["gallons"];

Thus, we can use these arrays to code something like:

The color paint you want is: <?php echo $_GET["color"]; ?>.<br />
You wish to order <?php echo $_GET["gallons"]; ?> gallons.

This bit of code would display:

The color paint you want is: Black.
You wish to order 8 gallons.

You may also wish to read the lesson on the $_POST variable