GET Method In Action: Simple Form

This Basic PHP tutorial illustrates the usage of GET method, with the help of a simple HTML form. It echo’s/print’s the user entered information on to the browser by passing it via GET method.
It also tells one big advantage of GET method over POST method.

Source Code
We have purposefully kept this example super simple, as this is a basic thing in PHP and complex things may confuse beginners.

Source Code: GET Method In Action: Simple Form

getForm.php

<html>
 <head><title>Form using GET method</title></head>
 <body>
<form action="get.php" method="get">
Name <input type="text" name="uname"><br />
Age    <input type="text" name="age"><br />
<input type="submit" value="Submit Info">
</form>
 </body>
</html>

This is a simple form which contains 2 input fields and a submit button. Observe the method used: its GET method.
We have given unique name to each input fields, so that the values are passed to the get.php

get.php

<?php
 
$name = $_GET['uname'];
$age     = $_GET['age'];
 
echo "Name is $name <br /> Age is $age";
 
?>

This is the actual file where the action takes place. All the information is passed on to this file via getForm.php file. Using $_GET[] we receive the values and store it in the local variables. Using the values in these local variables we can do whatever we want to do: like print it on the browser(as we do in this tutorial), insert it into database etc.

Video Tutorial: GET Method In Action: Simple Form



YouTube Link: https://www.youtube.com/embed/3zmH6zXyrx8 [Watch the Video In Full Screen + HD]


Advantages of using GET method over POST method

One of the biggest advantage of using GET method is, we can send the URL in the address bar to a friend over email as it contains the name and associated value in the URL. But in post method, you will neither get the name nor the value in the address bar, hence you can’t send it over to any one. If you still send, it may not make any sense!

Ex: You Google for the term “technotip”. Copy the URL in the address bar and can send it to your friend. Many bookmarking sites use GET method, because users must be able to copy and paste the url and pass it on to their friends. If they used POST method, then its of no use to send the url, since the URL before and after querying remains same!