How HTML Forms Work
A very common way to pass data from one page to another is through HTML forms. There are two methods of submitting data through a form: the get method and the post method. The method used is determined by the value of the
method attribute of the
form tag. The default method is get.
When the get method is used, data is sent to the server in name-value pairs as part of the query string. The get method is most commonly used by search pages and is useful when it is important to be able to bookmark the resulting page (i.e, the page that is returned after the form is submitted).
When the post method is used, data is sent to the server in name-value pairs behind the scenes. The two major advantages of the post method are:
- The name-value pairs are not visible in the location bar, so sensitive data such as passwords are not displayed on the screen.
- Files, such as images and Office documents, can be uploaded via the form.
The major disadvantage is that the resulting page cannot be bookmarked.
A Sample HTML Form
The following is a sample HTML form for inserting an employee record into a database.
The above code, which contains some embedded PHP code, outputs a simple HTML form. Its action page is ProcessEmployee.php, which will eventually contain PHP code to process the submitted form.
Form variables are variables that are created when an HTML form is submitted using the post method.
These variables are stored in the
$_POST superglobal array. Here are a few things you should know about how HTML forms are processed:
- If a text field or textarea is left blank, the variable is sent to the server with a value of "" (empty string).
- Select menus always send a variable to the server (unless they have been disabled in some way).
- If no radio button in a radio array is checked, no variable for that radio array is sent to the server.
- If a checkbox is not checked, no value is sent to the server. If it is checked, the default value of "on" is sent unless otherwise set in the HTML.
- If the submit button has a name (e.g,
<input type="submit" name="Inserting" value="Add Employee"/>), when that submit button is clicked a variable by that name with the corresponding value will be sent to the server.