Punjabi revolution tutorials PHP Form Submision POST and GET Methods

POST and GET methods using PHP

One of the most handy benefits of server side languages is, we can post information from one page to next. All the pages of a website are independent of each other, so they can not access the data of each other. Web provides us two methods to send information from one page to other.

  1.  get method
  2.  post method

GET method

In get method, the information is passed using URLs. For instance, when we search the query in google, we can find the searched query in google’s url in next page. Let’s say, we search the word ‘pakistan’ in google. the resulting page would contain the word pakistan in its url as below.

https://www.google.com.pk/?gws_rd=cr,ssl&ei=etmyV6viDIHbsgH2-4eoDw#q=pakistan

Advantages of using get method

1. you can book mark the url as it opens the result page without demaning any further information.
2. The browsers save the page in cache , so whenever, the page is fetched the next time, it is loaded faster.
it is also indexed by the google as an independent link thus a plus for SEO of you web site.

Disadvantages of using get method.

1.It has serious security concern as the information is shown in url. if the information contains credentials like email and password, the get method should never be used.
2 It is suited only for select queries. if we use it for update delete or insert operation, it can be misused.
3 It has limitation for the amount of data being sent as it is dependent on url

POST method

In post method, the data is transferred behind the scene. The url does not contain any information. For example when you sign into your gmail account, your email and password are not passed through the url. so when copy and paste the url in some other browser, it redirects to login page again.

Advantages of using post method

1 It does not have any size limitation for data transfer as it is not dependent on url.
2 It does not have any risk of security breach. Your sensitive information is neither visible nor cached nor logged anywhere.

Disadvantages of using post method
1 We can not bookmark the page.
2 Google can not index the url as an independent page as it would redirect to another page, thus it is not good for SEO.

Example of GET method.

create a file named form_get.html and place in htdocs folder of xampp or www folder of wamp.

Line 6: action attribute specifies the page where we would land after pressing the submit button. Method attribute shows the post method, here we are using the GET method.

Line 12: it is mandatory that every form should contain  a submit button because only submit button submits the form and takes you to the action page.

Now create an action page named “form_get_action.php” where we would land after submission and place the file in htdocs folder.

Line 4: The $_GET is an associative built in array to hold the values of the form submitted using GET method. Here the keys are the names of the form fields.

Example of POST method.

create a file named fform_post.html and place in htdocs folder of xampp or www folder of wamp.

Line 6: action attribute specifies the page where we would land after pressing the submit button. Method attribute shows the post method, here we are using the POST method.

Line 12: It is mandatory that every form should contain  a submit button because only submit button submits the form and takes you to the action page.

Now create an action page named “form_get_action.php” where we would land after submission and place the file in htdocs folder.

Line 4: The $_POST is an associative built in array to hold the values of the form submitted using POST method. Here the keys are the names of the form fields.

That’s all! happy coding.

Leave a Reply

Your email address will not be published. Required fields are marked *