CPSC 343 | Database Theory and Practice | Fall 2024 |
This document is intended to be an extremely brief introduction to HTML forms. Many details are omitted or simplified (e.g. not all syntax variations are presented). For more information, check out the HTML Tutorial from W3Schools.
See the HTML/PHP Local Quick Start for details on what to name your HTML files and how to view them.
HTML forms allow a user to input information which is then sent to the webserver for processing. There are many different kinds of form elements, intended for different kinds of input.
A form is denoted by the <form> tag:
<form action="action.php" method="POST"> ... </form>
The action attribute specifies the name of the script (on the webserver) that will process the data entered in the form when the form is submitted; the method attribute specifies how the data is sent to the webserver. There are two choices for the method, "POST" and "GET". (a discussion of POST vs GET)
The form tag itself doesn't generally produce any visible effect — it's what replaces the "..." between the <form> and </form> tags that defines what the form looks like and what can be entered. Pretty much any tag that can occur in the body of an HTML document can go inside a form, so you can use paragraphs, headings, lists, tables, etc to format the form.
The other ingredients in a form are form elements — these are what allow the user to enter information.
Each form element has a name attribute. It is very important to specify a name — this is how PHP will be able to access the values the user entered into the form. In general each form element should have a different name (exceptions are noted below).
Input fields are the most basic form of input, allowing the user to enter some quantity of text.
purpose | syntax/example | what it looks like |
---|---|---|
single-line input |
<input type="text" name="input1" value="default text" size="25">
|
|
multi-line input |
<textarea rows="3" cols="25" name="input2" wrap="soft"> default text </textarea>
|
A drop-down list allows the user to choose one option from a predefined list of options, presented in a drop-down list format.
purpose | syntax/example | what it looks like |
---|---|---|
drop-down list |
<select name="input3"> <option value="NY"> New York <option value="MA"> Massachusetts <option value="VT"> Vermont </select>
|
Both radio buttons and checkboxes allow the user to choose one or more options from a predefined collection, but with all the choices presented at once (instead of in a drop-down list format). Radio buttons only allow one choice to be selected at a time, and will automatically unselect a previously-selected choice if a new one is made. Checkboxes allow multiple choices to be checked simultaneously.
purpose | syntax/example | what it looks like |
---|---|---|
radio buttons (only one button in a group can be selected at a time) |
<input type="radio" name="input4" value="yes" checked> yes <br> <input type="radio" name="input4" value="no"> no <br> <input type="radio" name="input4" value="maybe"> maybe
|
yes no maybe |
check boxes (more than one button can be checked at a time) |
<input type="checkbox" name="input5[]" value="yes" checked> yes <br> <input type="checkbox" name="input5[]" value="no"> no
|
yes no |
Note that setting the "value" attribute does not cause labels to appear for the boxes — you must supply your own.
Every form needs at least a submit button, so the user can send the form data to the server for processing.
purpose | syntax/example | what it looks like |
---|---|---|
submit the form (send to the server for processing) |
<input type="submit" value="submit form">
|
|
clear user inputs in the form |
<input type="reset" value="reset form">
|
A use for hidden elements is described in the Hidden Form Elements section of the PHP Web Quick Start.
purpose | syntax/example | what it looks like |
---|---|---|
hidden element (nothing is displayed on the screen, but the value of the hidden element is submitted when the form is submitted, just like visible elements) |
<input type="hidden" name="input6" value="secret message"> |