Get vs. Post in PHP with Examples


The GET and POST are the two highly used methods in web applications where users submit information over the website. HTML forms usually have some input fields to extract valuable information from the visitors. Either you want to send this information to any other page, or database you need to specify a method which defines the mechanism to transfer the data.

The GET Method using PHP

It appends the encoded information to the URL of the page.

  • The GET method produces a long string by appending information to it.
  • You can send up to 1024 characters to the server using this method.
  • Get method isn’t suitable to transfer secure data like password etc.
  • This method can’t be used to upload files or images on the server
  • The PHP gives $_GET associate arrays to access all the information sent
  • As data is part of URL, it remains in the cache, and the whole URL can be bookmarked
Example of Get
<!DOCTYPE html>
<html>
<head>
  <link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
<style>
*{box-sizing:border-box;}
body{font-family: 'Open Sans', sans-serif; color:#333; font-size:14px; background-color:#dadada; padding:100px;}
.form_box{width:400px; padding:10px; background-color:white;}
input{padding:5px; width:100%; margin:5px 0;}
.shadow{
  -webkit-box-shadow: 0px 0px 17px 1px rgba(0,0,0,0.43);
-moz-box-shadow: 0px 0px 17px 1px rgba(0,0,0,0.43);
box-shadow: 0px 0px 17px 1px rgba(0,0,0,0.43);}
.pic{text-align:left; width:33%; float:left;}

</style>

<body>
 <div class="form_box shadow">
   <form class="" action="practice_ac.php" method="GET">
     <h4>THE GET METHOD</h4>
     <p>Enter your name!</p>
     <input type="text" name="query" value=""><br/>
     <input type="submit" name="submit" value="Submit">
   </form>
 </div>
</body>
</html>

After you click on “submit” button, the form’s data will be submitted to practice_ac.php
The URL would be something like the following: 
practice_ac.php?query=Your+name&submit=Submit

After you click on “submit” button, the form’s data is submitted to practice_ac.php
The URL would be something like the following:
practice_ac.php?query=Your+name&submit=Submit

Get URL

The URL after the question mark “?” is consisted of variables/value pair from the information being submitted.

Practice_ac.php will have the following code to print the values sent via GET method

<h4>This is how you will get $_GET array</h4>
<pre>
  <?php
  print_r($_GET); 
  ?>
</pre>
Get Method Result

The Post Method using PHP

This method transfers user information via HTTP headers.

  • The limit being enforced on the data being sent via POST method is described by the directives in php.ini and not by the POST method itself
  • The POST method can be used to send ASCII and binary data
  • The data transmitted by the POST method goes through the HTTP header, so it is considered a secure way.
  • The PHP provides $_POST associate array to access all the information.

To send the form’s data using the post method, change method = “post” in the above HTML form.

Example of Post
   <form class="" action="practice_ac.php" method="POST">
…..
   </form>

And replace $_GET with $_POST tin practice_ac.php to print the post array.
print_r($_POST);
You would see a clean URL this time. No variables are getting appended to practice_ac.php

Post Method Result

 

Related Articles

Create a Feedback form using PHP

Create a Login Page Using PHP and MySQL

Validating the checkbox in PHP

Leave a Reply