Introduction

Using the GET and POST methods, the browser client can send data to the web server. In PHP the GET and POST methods are used to retrieve information from forms, such as user input. Get and Post are methods used to send data to the server. These methods are used for data handling in forms. Where each method varies a little in the way they work.

GET Method

In the GET method, we can also send data to the server. But when using the GET method we can't send the login details with a word because it is less secure (information sent from a form with the GET method is visible to everyone). We can say that the GET method is for getting something from the server; it doesn't mean that you cannot send a parameter to the server.

The main points about the GET method are as follows:

Example

First of all we create a PHP file which is called by the HTML page in later.

<html>
<
body bgcolor="pink">
Welcome <?php echo $_GET ["Name"]; ?>.<br/>
You are <?php echo $_GET ["Class"]; ?> Qualified !!
</body>
</html>

The above file is to be saved with the name "get.php", which is called by the HTML page later.

<html>
<
body bgcolor="pink"><table>
<
form action="get.php" method="get">
<tr><td>Name: <input type="text" name="Name" /></td></tr>
<tr><td>Class : <input type="text" name="Class" /></td></tr>
<input type="submit" " value="Submit"/>
</form>
</body>
</html>

This file is saved as "get.html". In this file get.php is called.

Output

You will put the URL in the web browser like as: http://localhost/FolderName/get.html.

image1.jpg

Now you will fill text like as name and class. When you click on the submit button then you will see your browser URL changed. You can see in the following image.

image2.jpg

POST Method

In PHP $_POST variable is used to collect values from a form sent with method="post". In the POST method one can request as well as send some data to the server. The POST method is used when one can send a long enquiry form. Using the POST method the login details with word can be posted because it is secure (information sent from a form with the POST method is invisible to everyone).

The main points about POST method are as follows:

Example

First of all we create a PHP file which is called by the HTML page later.

<html>
<
body bgcolor="pink">
Welcome <?php echo $_POST ["Name"]; ?>.<br/>
You are <?php echo $_POST ["Class"]; ?> Qualified !!
</body>
</html>

The above file is saved with the "post.php" name, which is called by the HTML page later.

<html>
<
body bgcolor="pink">
<table>
<
form action="post.php" method="post">
<tr><td>Name: <input type="text" name="Name" /></td></tr>
<tr><td>Class : <input type="text" name="Class" /></td></tr>
<input type="submit" " value="Submit"/>
</form>
</body>
</html>

This file is saved as "post.html". In this file post.php is called.

image3.jpg

Now you will fill text like as name and class. When you click on the submit button then you will see your browser URL is not changed. You can see in the following image.

image4.jpg

$_REQUEST Variable

The $_REQUEST variable is also used to send data to the server. The $_REQUEST variable is a predefined variable in PHP. Which keeps the $_GET, $_POST and also $_Cookie variable. This variable is used to collect data of the form sent by the GET and POST methods.

First of all we create a PHP file which is called by the HTML page later.

<html>
<
body bgcolor="pink">
Welcome <?php echo $_REQUEST ["Name"]; ?>.<br/>
You are <?php echo $_REQUEST ["Class"]; ?> Qualified !!
</body>
</html>

The above file is saved with the "request.php" name, which is called by the HTML page later.

<html>
<
body bgcolor="pink">
<table>
<
form action="request.php" method="post">
<tr><td>Name: <input type="text" name="Name" /></td></tr>
<tr><td>Class : <input type="text" name="Class" /></td></tr>
<input type="submit" " value="Submit"/>
</form>
</body>
</html>

This file is save as "request.html". In this file post.php is called.

image5.jpg

If we use the POST method then the URL of the browser is not append. You can see in the following image.

image6.jpg

If we use the GET method then the URL of the browser is changed. You can see in the following image.

image7.jpg

Conclusion

So in this article you saw the differences between the GET and POST methods in PHP.

Some Useful Resources