When we want some feed back from the user on some subject or want some information to be supplied by the user, we take the help of forms. A form consists of certain input elements to accept user data that is then passed to another PHP script for further processing on selecting Submit button. In this article, we will learn different HTTP Request methods like GET and POST that are used for sending data from one PHP script to another.
<form action=”display.php” method=”get”>
Name: <input type=”text” name=”name” />
Age: <input type=”text” name=”age” />
<input type=”submit” />
Above form has two input boxes named “name” and “age”. The form “action” points to a php file : “display.php” and the “method” is “get”. Meaning the contents of the form will be submitted to php script : “display.php” using the HTTP request-method GET for further processing.
The information is passed from one PHP script to another using any of the following two HTTP Request methods :
In this method the information passed is less secure as it is displayed in the browser’s address bar and moreover, there is a limit on the amount of information passed (it can be of maximum 100 characters).
In this method, the information passed is more secure as it is not displayed in the browser’s address bar.
The $_GET array is an array where data from the previous form sent using HTTP GET method are stored. The data from the previous form is sent in the form of pairs : variable name(s) and its value(s).
Recall that information sent from a form with the GET method is less secure as it is displayed in the browser’s address bar and also the size of the information passed is limited.
When the user clicks the “Submit” button, the URL sent will look something like this:
We can see that when we use the GET method, all the information passed is attached in the browser address bar after the ? mark.
The target file : “display.php” file can now extract the data from $_GET array using following statements :
Welcome <?php echo $_GET[“name”]; ?>.<br>
Your age is <?php echo $_GET[“age”]; ?>
The name and age sent from the source PHP script is accessed from $_GET array and displayed .
The $_POST array is an array which collects the values sent from a form using HTTP POST method.
<form action=”display.php” method=”post”>
Enter your name: <input type=”text” name=”name” />
Enter your age: <input type=”text” name=”age” />
<input type=”submit” />
When the user clicks the “Submit” button, the $_POST[“name”] and $_POST[“age”] variables will be automatically set by PHP. The $_POST array contains all POST data and the URL will not display any form data (so it is more secure).
The destination PHP script : “display.php” file can now use the $_POST array to retrieve the form data with the help of following code :
Welcome <?php echo $_POST[“name”]; ?>.<br>
Your age is <?php echo $_POST[“age”]; ?>
Note : Information sent from a form with the POST method is invisible to others and also there is no limit to the amount of information sent.
To conclude : HTTP POST method is a better way of transmitting data because :
- Variables sent with HTTP POST are not shown in the URL
- Variables have no length limit
$_REQUEST array contains the contents of both $_GET and $_POST. That is, it is used to collect the information from a form which is sending data by either method : GET or POST method.
So, in case we are not known of HTTP method used by the source PHP script, it is a wise decision to access the information using $_REQUEST array. The target file : display.php made in the earlier examples can be rewritten as :
Welcome <?php echo $_REQUEST[“name”]; ?>.<br />
Your age is <?php echo $_REQUEST[“age”]; ?>
It will access the information sent from the source PHP file sent by any method : GET or POST
For more information, refer my book: “Developing Web Applications in PHP and AJAX” available at: http://www.amazon.com/Developing-Web-Applications-PHP-AJAX/dp/0070707103/ref=ntt_at_ep_dpt_4