There exist two methods in PHP to collect data submitted in a FORM. PHP GET Method and PHP POST Method.
In the PHP POST method, data from HTML FORM is submitted/collected using a super global variable $_POST. This method sends the encoded information embedded in the body of the HTTP request and hence the data is not visible in the page URL unlike the GET Method.
Advantages of Using POST Method
Some of the advantages of using the POST Method are:
- POST Method can send data without any limitation on size.
- Since the encoded information is embedded in the body of the HTTP request, it is not visible in the URL, hence the POST Method is preferred while sharing sensitive information.
- It can also send binary data with the help of POST Method.
Disadvantages of Using POST Method
Some of the disadvantages of using this method are:
- Since it does not embed data submitted by the POST Method in the URL, hence it is not possible to bookmark the page.
- POST requests do not get stored in browser history.
- POST requests are not cached.
Difference Between PHP GET and POST Method
Although both GET and POST methods are used to collect/submit form data, their methods exhibit some differences and are hence used according to need.
Some of the main differences between them are:
Method |
GET |
POST |
Data Security |
Not Secure |
Secure |
Data Limit |
2048 Characters |
No Limit |
Performance |
Good |
Slower |
Data In URL |
Visible |
Not Visible |
Data Types |
Only String Data Types |
String, binary, numeric, etc. |
Cacheable |
Yes |
No |
Example of POST Method Code
To understand how $_POST collects data, create a simple FORM with two fields - Name and Age, and collect and display that data using the POST Method.
<?php
if(isset($_POST["name"]) || isset($_POST["age"]) ) {
if (preg_match("/[^A-Za-z'-]/",$_POST['name'] )) {
die ("invalid name and name should be alpha");
}
echo "Hello ". $_POST['name']. "<br />";
echo "Age: ". $_POST['age']. " years old.";
exit();
}
?>
<html>
<body>
<form action = "<?php $_PHP_SELF ?>" method = "POST">
Name: <input type = "text" name = "name" />
Age: <input type = "text" name = "age" />
<input type = "submit" />
</form>
</body>
</html>
In the above code, the form method has been set to “POST” while the PHP script collects the form data using the super global variable $_POST.
Below is the form with the Name and Age fields. Notice the URL before submitting the form does not contain any information.
Now, look at the URL of the webpage. The POST Method, as discussed earlier, does not display any information on the URL, instead, the encoded data is embedded in the body of the HTTP request.
This is how the POST Method is used to collect and display information.
Conclusion
This brings us to the end of the “PHP POST Method” tutorial. In this, you have learned what the POST method is, and the $_POST superglobal variable. The advantages and disadvantages of using this method. You looked at the difference between GET and POST Methods and finally an example of how the POST Method is used with the help of a simple HTML FORM.
You can refer here for a video tutorial on the GET & POST Methods.
Are you planning to take the plunge and do a course on PHP? In that case, Simplilearn’s PHP course would be an excellent choice. The Post Graduate Program in Full Stack Web Development covers all the fundamental and advanced concepts in PHP, making your journey towards learning PHP an easy one.
If you have any queries regarding the PHP POST Method tutorial, do mention it in the comment section of this tutorial, and we’ll have our experts answer them for you.
Happy Learning!