PHP Tutorial 12 | Working with Forms, $_POST, $_GET, and $_REQUEST Variables
18 November 2007 - 1:36
In PHP Tutorial 11 we covered the basics of working with forms in PHP. In that tutorial we created a simple “contact us” form. We used the POST method in our HTML. In this section we are going to discuss the variables that relate to both the POST and GET methods. Those are $_POST, $_GET, and $_REQUEST variables. By the time you are finished with this tutorial you’ll have a solid understanding of how and when to use each.
$_POST and $_GET
You may already have an idea about how these two variables work from your experience with HTML. The fundamental difference between GET and POST is that GET can be encoded into a URL while POST is invisible. The variables $_POST and $_GET relate to the specific methods. If you use the POST method in your form then you would use the $_POST variable to retrieve and array of each of the different values sent in the form. If you used the GET method you would use the $_GET variable to retrieve the array of values.
When to Use POST and When to Use GET
In it’s simplest form people use GET for retrieving data, and they use POST for sending data. For example GET is common in search forms. With get you can store the request in a URL string. This means that it can be bookmarked. So if you created a search and wanted to bookmark it so you could always get to the results page you could. One other point to note is that the GET URL string can only contain 100 characters. POST on the other hand can contain an unlimited number of characters and since it is not something that you see in a URL string, it is often used for sending things like passwords, emails, or communications that you don’t want to show up in a URL string.
Here is an example:
<form action="usr_details.php" method="post"> First Name: <input type="text" name="first_name"> Last Name: <input type="text" name="last_name"> Email Address: <input type="text" name="email"> <input type="submit"> </form>
Saving this form and opening it up in a browser you’ll notice that when you click on submit you see only:
http://www.YOUR_URL.com/usr_details.php
None of the information submitted in the form shows up in the URL. If you had used the GET method you would see something like this instead:
http://www.YOUR_URL.com/usr_details.php?first_name=Bob&last_name=Smith&email=bsmith%40test.com
In the PHP script that we use to process the form data (usr_details.php) we simply use the $_GET and $_POST variables where appropriate depending on which method you used (POST or GET). If we were using POST and wanted to print the data that was sumbited on screen we would code something like this:
<?php
echo "<p>".$_POST["first_name"]."<br />";
echo $_POST['last_name']."<br />";
echo $_POST['email']."</p>";
?>
If we were using GET and wanted to print the data that was sumbited on screen we would code something like this:
<?php
echo "<p>".$_GET["first_name"]."<br />";
echo $_GET['last_name']."<br />";
echo $_GET['email']."</p>";
?>
Again, the big difference is that when the form is submitted and processed the URL string that shows is either something like:
http://SOME_URL.com/usr_details.php?first_name=Bob&last_name=Smith&email=bsmith%40test.com
Or if you used post it would look something like:
http://SOME_URL.com/usr_details.php
The $_REQUEST Variable
The last variable that you can use is the $_REQUEST variable. This variable is very useful because it contains information related to both POST and GET along with any cookies that have been set. We’ll discuss cookies in more detail in later tutorials. For now just know that you can use the $_REQUEST variable to retrieve information on both GET and POST along with cookies. Similar to $_GET and $_POST the code looks like this:
<?php
echo "<p>".$_REQUEST["first_name"]."<br />";
echo $_REQUEST['last_name']."<br />";
echo $_REQUEST['email']."</p>";
?>
[ Go back to PHP Tutorial 10 | Using External File Includes in PHP ]
2 Comments | Tags: PHP

Loading ...



