PHP 5 Form Handling

This tutorial discusses PHP form handling/processing. You will learn how to collect user-supplied form data using POST and GET method from various from controls like text, select, radio, checkbox and textarea

Typical structure of an HTML form intended to submit data to a PHP file looks like following:

Example -

When user submits data by clicking on "complete reservation", data supplied to various fields of the form goes to the file mentioned as a value of the action attribute specified within the opening tag of the form. For this example, like often developers do, we have used this file itself for processing form data. So, points to the file itself. If you wish to use a PHP file other that this to process form data, you may replace that with filename of your choice.

Form data is submitted to the PHP file for processing using POST here. Other methods you may use instead of POST is GET and REQUEST.

In a moment we will see differences between those three methods.

GET vs POST vs REQUEST

All of these methods create an associative array(e.g. array( key => value, key2 => value2, key3 => value3, ...)). This array holds key-value pairs, where keys are names of the form controls and data supplied to those controls are values.

Both GET, POST and REQUEST are treated as $_GET, $_POST and $_REQUEST. All of these are superglobals,i.e. automatically global variables. So, they can be accessed throughout a script, including within functions or methods, without declaring them explicitly as global $variable;.

$_GET is an associative array of variables passed to the current script via the URL parameters.

$_POST is an associative array of variables passed to the current script via the HTTP POST method.

$_REQUEST is also an associative array. Since it contains the contents of $_GET, $_POST and $_COOKIE automatically, it can be modified remotely and thus can't be trusted. So, it may be not a good idea to use REQUEST when processing form data. so, you may use GET or POST.

GET vs. POST

Both GET and POST create an array (e.g. array( key => value, key2 => value2, key3 => value3, ...)). This array holds key/value pairs, where keys are the names of the form controls and values are the input data from the user.

Both GET and POST are treated as $_GET and $_POST. These are superglobals, which means that they are always accessible, regardless of scope - and you can access them from any function, class or file without having to do anything special.$_GET is an array of variables passed to the current script via the URL parameters.

$_POST is an array of variables passed to the current script via the HTTP POST method.