How can get form data using post method in php?

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.

   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']. "
";

      echo "Age: ". $_POST['age']. " years old.";    

      exit();

   }

?>

    

      

         Name:

         Age:

         

      

 

   

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.

How can get form data using post method in php?

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.

How can get form data using post method in php?

This is how the POST Method is used to collect and display information.

Looking forward to becoming a PHP Developer? Then get certified with the Advanced PHP Development Course. Enroll now!

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 PHP certification 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! 

How can I GET form data in POST request?

To post HTML form data to the server in URL-encoded format, you need to make an HTTP POST request to the server and provide the HTML form data in the body of the POST message. You also need to specify the data type using the Content-Type: application/x-www-form-urlencoded request header.

How can I access form data in PHP?

$_POST['firstname']: The form data is stored in the $_POST['name as key'] variable array by PHP since it is submitted through the POST method, and the element name attribute value – firstname (name=”firstname”) is used to access its form field data.

How do you GET information from a form that is submitted using the POST method?

The Correct Answer is " Request. Form". The Request. Form command is used to collect values in a form with method="post".

What is form method POST in PHP?

PHP $_POST is a PHP super global variable which is used to collect form data after submitting an HTML form with method="post". $_POST is also widely used to pass variables. The example below shows a form with an input field and a submit button.