PHP POST and GET Variables Tutorial
Ranked #2,503 in Internet, #145,129 overall
What are they and what are they used for?
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.
$_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
}
?>
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.
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 ...
Related Products (Amazon)
Related Products(eBay)
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 »
- 21 featured lenses
- Winner of 14 trophies!
- Top lens » PHP & MySQL Poll System Tutorial
Explore related pages
- PHP & MySQL Poll System Tutorial PHP & MySQL Poll System Tutorial
- PHP & MySQL Login System Tutorial PHP & MySQL Login System Tutorial
- PHP & MySQL Visitors Counter Tutorial PHP & MySQL Visitors Counter Tutorial
- PHP basics tutorial PHP basics tutorial
- PHP Banner Rotator Tutorial PHP Banner Rotator Tutorial
- PHP Sessions and Cookies Tutorial PHP Sessions and Cookies Tutorial