Tuesday, March 10, 2020

HTML Form and difference between GET and POST Methods

What is Form?

When you login into a website or into your mail box, you are interacting with a form. Forms are used to get input from the user and submit it to the web server for processing. The diagram below illustrates the form handling process.
A form is an HTML tag that contains graphical user interface items such as input box, check boxes radio buttons etc.
The form is defined using the <form>...</form> tags and GUI items are defined using form elements such as input.

HERE,
  • <form…>…</form> are the opening and closing form tags
  • action="registration_form.php" method="POST"> specifies the destination URL and the submission type.
  • First/Last name: are labels for the input boxes
  • <input type=”text”…> are input box tags
  • <br> is the new line tag
  • <input type="hidden" name="form_submitted" value="1"/> is a hidden value that is used to check whether the form has been submitted or not
  • <input type="submit" value="Submit"> is the button that when clicked submits the form to the server for processing

Submitting the form data to the server

The action attribute of the form specifies the submission URL that processes the data. The method attribute specifies the submission type.

PHP POST method

  • This is the built in PHP super global array variable that is used to get values submitted via HTTP POST method.
  • The array variable can be accessed from any script in the program; it has a global scope.
  • This method is ideal when you do not want to display the form post values in the URL.
  • A good example of using post method is when submitting login details to the server.
It has the following syntax.
<?php
 $_POST['variable_name'];
?>
  HERE,
  • “$_POST[…]” is the PHP array
  • “'variable_name'” is the URL variable name.

PHP GET method

  • This is the built in PHP super global array variable that is used to get values submitted via HTTP GET method.
  • The array variable can be accessed from any script in the program; it has a global scope.
  • This method displays the form values in the URL.
  • It’s ideal for search engine forms as it allows the users to book mark the results.
It has the following syntax.
<?php
$_GET['variable_name'];
?>
  HERE,
  • “$_GET[…]” is the PHP array
  • “'variable_name'” is the URL variable name.

GET vs POST Methods

POST

GET

Values not visible in the URL

Values visible in the URL

Has not limitation of the length of the values since they are submitted via the body of HTTP

Has limitation on the length of the values usually 255 characters. This is because the values are displayed in the URL. Note the upper limit of the characters is dependent on the browser.

Has lower performance compared to Php_GET method due to time spent encapsulation the Php_POST values in the HTTP body

Has high performance compared to POST method dues to the simple nature of appending the values in the URL.

Supports many different data types such as string, numeric, binary etc.

Supports only string data types because the values are displayed in the URL

Results cannot be book marked

Results can be book marked due to the visibility of the values in the URL

The below diagram shows the difference between get and post
PHP Form

  PHP Form