Domesticated Brain

PHP - GET and POST Methods 

 

GET and POST methods are the two types of commonly used HTTP request methods in PHP to submit form date.

If further defined, it’s the way of sending data to be processed at the server side from web browser to the server.

Let’s find out about the GET and POST method separately and where it should be included in the PHP programming.

In this tutorial we are working with the following html form which has 2 text fileds for first name , last name and the submit button to submit the data

 

<form action="" method="">

      First Name: <input type="text" name="first_name"> <br><br>

      Last Name: <input type="text" name="last_name"> <br><br>

      <input type="Submit">

</form>

 

PHP Form for GET and POST Method 

PHP - GET Method

PHP GET method sends data in the URL of the HTTP request  

<form action="index.php" method="GET">

   First Name: <input type="text" name="first_name"> <br><br>

   Last Name: <input type="text" name="last_name"> <br><br>

   <input type="Submit">

</form>

 

PHP Form GET Method

 

It will be shown in the web browser as follows

 

GET Method Result

We have sent the data in the URL of the HTTP request which are shown by the name keys and the values that we have submitted recently in the query string parameters

Generally, programmers avoid using of GET Method in php to send sensitive information to the server because it’s visible to the user

 

PHP - POST method

PHP POST method is the most appropriate method to submit sensitive date because data is not visible in the URL .Also you can handle more data than the GET method via the HTTP request because it sends data in the message body of the HTTP request.

<form action="index.php" method="POST">

   First Name: <input type="text" name="first_name"> <br><br>

   Last Name: <input type="text" name="last_name"> <br><br>

   <input type="Submit">

</form>

 

 PHP Form POST Method 

 

After submitting form data, you can see name key without any values in the URL

 

POST Method Result

 

Because it’s send data through the Body of the HTTP request rather than sending through the URL  

 

Let’s check the submitted data using PHP POST method which are available for us to process

For this instance we are submitting the data to same PHP file index.php

To do this we have to use ISSET construct in PHP programming 

<?php

 

if(isset($_POST['first_name'], $_POST['last_name'])){

   echo "Infromation submited is available to be processed";

}

 

?>

 

<form action="index.php" method="POST">

   First Name: <input type="text" name="first_name"> <br><br>

   Last Name: <input type="text" name="last_name"> <br><br>

   <input type="Submit">

</form>

 

PHP POST Method With ISSET Construct

Since the above program doesn’t give us an error massage, the information we have recently submitted can be processed.

If you have found  what you expected please comment and like on our facebook page