PHP Post

$_POST is a variable which collects values from a form.

You may also wish to read the lesson on the $_GET variable.

You may have seen forms which look something like this:

<form action=”form.php” method=”post”>

Forms such as these are utilizing the PHP $_POST variable.

Differences Between GET and POST

There are some important differences between the ways GET and POST work.

For example, GET only allows a maximum size of 100 characters, whereas POST has no limit.

We also learned then when using GET, the elements entered in the form become part of the URL, and are visible to others. For example:

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

On the other hand, information gathered from the form using POST will not become part of the URL, thus are not visible to others.

POST Example

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

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

When the user clicks the “Send!” button, the data gathered (color and gallons) will be sent to our form.php file without being visible in the URL.

The names of the form fields will each be a key in our POST associative array as so:

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

You may also wish to read the lesson on the $_GET variable.