PHP Form Handling : Get


The form (html form or web form) on a page allows a user to enter data and send it to server for processing. In PHP the submitted data can be accessed by using superglobal variables which are $_GET, $_POST and $_REQUEST. A simple example of html Form is given below :
<html>
<body>
  <form action="file.php" method="get">
    Name : <input type="text" name="name"/>
    Age : <input type="number" name="age"/>
    <input type="submit" value="Submit"/>
  </form>
</body>
</html>


From the client side the data is sent to the server using HTTP GET and POST methods.

The GET Method :

The GET method is default method of html form. In GET method  the data is sent to the server as URL parameters that are usually strings of name and value pairs separated by ampersands (&). For example a URL with GET data will look like below :
 http://myserver.com/file.php?name=john&age=24
At the above example, the bold part of the URL are the parameters, which are in parameter=value format. The two parameter=value pairs are separated by &  (ampersand) sign, where name is parameter and key john is value, age is parameter and 24 is value.

The PHP provides the superglobal variable $_GET to access all the information sent either through the URL or submitted through an HTML form using the method="get". The GET method create an associative array, which holds the data in key/value pairs, where key are the names of the form fields and values are the input data from the user. The variable $_GET Contains a list of all the field names and values sent by form using the GET method, which can be accessed by the name of fields :
 $_GET['field_name'];
For example, we can access the above mentioned form data with the below code
 $_GET['name'];
 $_GET['age'];
Now lets see the below Example code :

form.php
<html>
<body>
  <form action="file.php" method="get">
    Name : <input type="text" name="name"/>
    Age : <input type="number" name="age"/>
    <input type="submit" value="Submit"/>
  </form>
</body>
</html>
file.php
<?php

  $name = $_GET['name'];
  $age = $_GET['age'];
  echo "Hello $name. <br/>";
  echo "You are $age years old."

?>
Output :




Use of GET Method :

Information sent from a form with the GET method is visible to everyone. All variable names and values are displayed in the URL. GET also has limits on the amount of information to send. The limitation is about 2000 characters. However, because the variables are displayed in the URL, it is possible to bookmark the page. GET method is not secure and hence not a suitable choice for transferring confidential data but, GET method is extremely useful for retrieving static content from web server. Here are some examples where a using GET method :

  • There is no side effect of repeated request. for example clicking a link which points to another page. it doesn't matter if you click the link twice or thrice , This also gives chance browser of server to catch the response for faster retrieval.
  • If data requires to be sent to Server is not large and can safely accommodated in maximum length of URL supported by all browser. In general,  different browser has different character limit for URL length but having it under limit is good choice.


Next Topic :