Image

PHP - PHP GET and POST Method - PHP GET and POST Method

PHP GET and POST Method

While dealing with the forms, information can be submitted and transferred to same or another page. To send submitted data through form, one can use GET&POST method to do that in PHP.

A form data can be submitted using these two methods. Both are used for same purpose but stands apart under some specifications. As in GET method key values are passed in the Url while in POST, the information transfers in a hidden manner.

A form submitted information is appended in to the url in the form of Query String consisting of name=value pairs in URL. This string contains user values/data, which are separated by ampersand and spaces are replaced with + sign.

GET Method

As explained above, before sending any information , it converts values/data into a query string in URL known as Url Encoding. Which contains both page link and encoded information separated by the ?character.

Client Side: Below code is an HTML form with method=”get” for user to fill information.

<form action="#" method="get">
<input type="text" name="name" placeholder="Your Name"></input><br/>
<input type="text" name="email" placeholder="Your Email"></input><br/>
<input type="text" name="contact" placeholder="Your Mobile"></input><br/>
<input type="submit" name="submit" value="Submit"></input>
</form>
O/P



Server Side: Below code has PHP script where, $_GET associative array is used to receive sent information at server end.

<?php
if( $_GET["name"] || $_GET["email"] || $_GET["contact"])
{
echo "Welcome: ". $_GET['name']. "<br />";
echo "Your Email is: ". $_GET["email"]. "<br />";
echo "Your Mobile No. is: ". $_GET["contact"];
}
?>
O/P


Above query string of information, generated by Get method can be readable in address bar therefore, never use Get method for sending sensitive information to server.

One should avoid use of this method to send binary data like, Images or Word Document file to the server.


POST Method

As explained above, before sending information to server, it converts client’s information into a query string in URL.

Client Side: Below code is an HTML form with method=”post” for user to fill information.

<form action="#" method="post">
....
</form>

Server Side: Below code has PHP script where, $_POST associative array is used to receive sent information at server end.

<?php
if( $_POST["name"] || $_POST["email"] || $_POST["contact"])
{
echo "Welcome: ". $_POST['name']. "<br />";
echo "Your Email is: ". $_POST["email"]. "<br />";
echo "Your Mobile No. is: ". $_POST["contact"];
}
?>
O/P


Query string , generated by Post method never appears in address bar i.e. it is hidden for the user therefore, we can use this method for sending sensitive information to server. Moreover, we can make use of this method to send binary data to the server without any restrictions to data size.

In our example, we allow user to choose a method via radio button and this value is assigned to form’s method attribute.

$("input[type=radio]").change(function(){
var method = $(this).val();
$("#form").attr("method", method);// Assigns Method Type From Radio Button
});