GET and POST Methods In PHP



In PHP there is the two main important method is used to send the form data these methods are the 1. The GET Method 2. The POST Method

For sending the data we use the HTML

tag, and in this tag methods are used that tells the form where and how the data is to be submitted. The data can be submitted in the form of a PHP script or a URL.

The both function is the same but the difference are only in terms of how the form data is submitted? the main differences are given below:

GET Method :
1. GET method is used to submit the data in the form of a URL.
2. Data submitted by the GET method is appears into the browser location box.
3. There is limit to send only the limited data, you can send only 1024 characters by using the GET method.
4. Data send by the GET method is not secure because it appears on the browser location box,
so please avoid to use the GET method while working on the Login or any other secure application.
5. You can not send the binary data by using the GET method, binary data means images,documents.
6. You can access the GET data by using the QUERY_STRING environment variable.

POST Method :
1. POST method is used to submit the data directly to the script or URL mentioned.
2. Data submitted by the POST method is not appears into the browser location box.
3. There is no limit to send the data, you can send unlimited amount of data by using the POST method.
4. Data send by the POST method is secure because it does not appears on the browser location box, so
there is no need to worry about it.
5. You can send ASCII as well as binary data by using the POST methods.
6. You can access the values of the POST Data by using the $_POST associative array predefined PHP method.

Use of the GET and POST methods in PHP scripts.


Here are the example, how we can use the GET and POST methods into the HTML?

<form action="index.php" method="post">
	Your custom code here
</form>
<form action="index.php" method="get">
	Your custom code here
</form>

If we are using the POST method then values will be declared as

$values = $_POST[‘values’];

If we are using the GET method then values will be declared as

$values = $_GET[‘values’];

Example of the GET method:

<form action="index.php" method="get">
	User Name: <input type="text" name="uname" />
	User Age: <input type="text" name="uage" />
	<input type="submit" value="Submit" />
</form>

Now, when you enters the information whics is as,
“User Name: EWA, User Age: 25” and clicks on the Submit buttons, then the resulting URL would be

http://test.com/index.php?uname=EWA&uage=25

You display this data on the index.php as the example given below:

<?php
	echo $_GET['uname']; // Output would be EWA 
	echo "<br>";
	echo $_GET['uage']; // Output would be 25
?>

Example of the POST method:

<form action="index.php" method="post">
	User Name: <input type="text" name="uname" />
	User Age: <input type="text" name="uage" />
	<input type="submit" value="Submit" />
</form>

Now, when you enters the information whics is as,
“User Name: EWA, User Age: 25” and clicks on the Submit buttons, then the resulting URL would be

http://test.com/index.php

You display this data on the index.php as the example given below:

<?php
	echo $_POST['uname']; // Output would be EWA 
	echo "<br>";
	echo $_POST['uage']; // Output would be 25
?>

This is all about the GET & POST methods in PHP, Hope this will be helpful, Thanks and keep reading this blog.