Difference Between the POST and GET Method in PHP

php


There are two methods which you can use to retrieve information from a web form, the POST and GET methods.

In this article, we discuss the difference between the POST and GET method, so you can know which one to utilize during information retrieval of form data.

Difference Between POST and GET Method


POST Method


The POST method retrieves information and can add it directly to a web form. Let's say you have the following web form below which asks for a user's first name, last name, email address, and telephone number.











The POST method can take all these filled-in fields of the form and write them to a web page, such as the confirmation page that this form creates when the submit button is pressed.

The POST method is the most used method when retrieving information from an HTML form using PHP.

GET Method


The GET method retrieves the information from a form and appends it to the URL of the website in use. Unlike the POST method, it cannot output information directly to a web page, but adds the user-added information from the form fields to the URL.

Below is the same exact form above but with the method of the form action attribute changed to "GET".











Once you fill in the form fields and press the submit button, just like before, it takes you to a confirmation page but does not print the data to the web page, like the POST Method.

However if you look at the URL, you can see the information appended to the URL. So, for example, if you typed in "Peter" as the first name, "Williams" as the last name, "PeterWilliams@yahoo.com" as the email address, and "800-489-7849" as the telephone number, you will see as output the URL:

http://learningaboutelectronics.com/Articles/Difference-between-the-post-and-get-method-in-PHP? FirstName=Peter&LastName=Williams&EmailAddress=PeterWilliams@yahoo.com&TelephoneNumber=800-489-7849 &form=Submit+Query

This URL broken down so that you can see better is:

http://learningaboutelectronics.com/Articles/Difference-between-the-post-and-get-method-in-PHP
?FirstName=Peter
&LastName=Williams
&EmailAddress=PeterWilliams@yahoo.com
&TelephoneNumber=800-489-7849
&form=Submit+Query

This is how the GET method works.

Because of its functions, it is used much less than the POST method. Also, because it appends all of the user's information to the URL, it is much less secure and safe to use than the POST method.


HTML Comment Box is loading comments...