PHP POST and GET Variables Tutorial

Ranked #2,503 in Internet, #145,129 overall

What are they and what are they used for?

POST and GET variables can be used to catch user input. POST variables are used to do this through forms ( catching the user input from input fields after a form has been submitted ) and GET variables are used to do this through the url a webpage is accessed by ( for example: website.com/index.php?page=home ). Both are arrays and can therefore contain multiple sub-variables, which is necessary to catch multiple user input data. For example: the user input for inputfield 1 and for inputfield 2 will both be stored inside the POST array but each having their own sub-variable in it.

Pre-Knowledge
To be able to follow this tutorial you should already have mastered the PHP basics.

POST Variables

How to use POST variables in PHP.

As mentoned above, POST variables are used to catch user input from html input fields ( forms ). Once a form is submitted, all values filled in for the input fields will be stored inside of the array $_POST. Each input field will have its own sub-variable in this array with as name the name of the input field and as value the value filled in inside of the input field. So once the user hits the submit button of a form, this will happen for all the input fields of the form:

$_POST['inputfield_name'] = "inputfield value filled in by user";

However, this happens only when the form is submitted. Before that, there isn't any $_POST array at all. We can thus check whether a form was submitted by checking whether the $_POST array exists. Have a look at this example:

test.php

<?php
if(isset($_POST)) { /form submitted?

echo "The POST variable was created so the form has been submitted!";
echo "You filled in ".$_POST['username']." for the input field called 'username'.";

}else{ //form hasn't been submitted yet -> show the form

?>

<form method="POST" action="test.php">
<b>Username:</b> <input type="text" name="username">
<input type="submit" value="Click me to submit the form">
</form>

<?php

}
?>

We check with a simple if loop whether the $_POST array exists (is set) and thus whether the form was submitted. Then we display the value that was entered inside of the input field 'username' by accessing the sub-variable that was automaticly created for it inside of the $_POST array (In this case: $_POST['username'] as 'username' was the name of the inputfield and we used the POST method to submit the form).

Note: do make sure the method of the form is set to POST, otherwise the form will be submitted using the GET method and all input fields and their values will be displayed inside of the url and created to the $_GET array instead of the $_POST array.

GET Variables

How to use GET variables in PHP.

GET variables are different from POST variables because, as mentioned before, they are shown inside of the url. However the $_GET variable is as well an array of sub-variables, just like the $_POST array. The $_GET array, though, can be created by adding variables to the url of the webpage. For example:

http://www.website.com/page.php?variable1=value1&variable2=value2&variable3=value3

Simply put a question mark (?) after the php file and then define any amount of variables (seperated by the & sign). All variables defined this way will be stored inside of the $_GET array. So when accessing page.php as shown above, the following code is generated automaticly for the $_GET array:

$_GET = array("variable1" => "value1", "variable2" => "value2", "variable3" => "value3");

Usually GET variables are used to tell a PHP file which webpage to display or which post ( on a forum for example ).

Note: Usually, when possible, the POST method is used because it's safer as the variables are hidden while the GET variables are displayed inside of the url and can be easily manipulated this way as well.

You might also like ...

Loading

Comments & Questions

Got any questions about the tutorial or just want to comment? This is the place to do so.

Related Products (Amazon)

Loading

Related Products(eBay)

Loading

by

webcodez

"Give a man a program and you'll frustrate him for one day. Teach a man to program and you'll frustrate him for a whole lifetime."

webcodez
GameHeroes
more »

Feeling creative? Create a Lens!