PHP $_GET and $_POST functions with forms

$_GET Method  |  $_POST Method

There are two types of methods are available for a form to send data. They are “get method” and “post method”.

 

PHP $_GET function is used for collecting the values passed from a “form using get method”. When we use get method, passed variables and its values will be displayed in browser address bar and visible for everyone.

Here is sample form using get method

<form action="form.php" method="get">
Name: <input type="text" name="yourname" />
Age: <input type="text" name="age" />
<input type="submit" value="SUBMIT FORM" />
</form>

Here you can see that values are passed to form.php using get method.
Now look at the form.php code with $_GET function

<?php
$name=$_GET["yourname"];
$age=$_GET["age"];
echo "Name :".$name."<br>";
echo "age".$age;
?>

Suppose user has entered the values JOHN for name and 25 for age in the first form. After pressing the submit button, form will send the data to form.php . That time address bar will look like
form.php?yourname=JOHN&age=25
In form.php passed values are retrieved using $_GET function and output will be like:
Name : JOHN
age : 25

Limitations of GET method :

  • Maximum characters can be passed is limited, so its not suitable for sending large data
  • passed data will be visible in browser, so in some cases it wont be suitable to use get method like passing password and other secret datas.

Advantages of GET method :

  • User can bookmark or direct access to the page with passed data. Next time he don’t need to enter the form again to access the same page
  • Search engines can index the page with passed data. SEO friendly method

 

PHP $_POST function is usedd for collection the values passed from a “form using post method”.There are two types of methods are available for a form to send data. They are “get method” and “post method”. $_POST function is applicable for post method only.
When we use post method, passed variables and its values will be hidden in browser address bar and wont visible for the users.

Here is sample form using post method

<form action="form.php" method="post">
Name: <input type="text" name="yourname" />
Age: <input type="text" name="age" />
<input type="submit" value="SUBMIT FORM" />
</form>

Here you can see that values are passed to form.php using post method.
Now look at the form.php code with $_POST function

<?php
$name=$_POST["yourname"];
$age=$_POST["age"];
echo "Name :".$name."<br>";
echo "age".$age;
?>

Suppose user has entered the values JOHN for name and 25 for age in the first form. After pressing the submit button, form will send the data to form.php . That time address bar will look like
form.php only ( see the passed values are hidden )
In form.php passed values are retrieved using $_POST function and output will be like:
Name : JOHN
age : 25

Advantages of POST method :

  • There is no limit for Maximum characters can be passed, It depends on the post_max_size variable stetting in your php.ini file. So its suitable for passing large data
  • passed data will be hidden in browser, so its suitable for passing password like secret datas

Dis Advantages of POST method :

  • User cannot bookmark or direct access to the page with passed data. Next time we needs to enter the form again to access the same page
  • Search engines cannot index the page , because the passed values are hidden and cannot be accessible
Share
Share
Share