Forms were also amongst the first additions to the HTML standard, and provide the ability to submit data to a web server. A web form is composed of <input>
, <textarea>
, <select>
and similar elements nested within a <form>
element.
The form element primarily is used to organize input elements and specify how they should be submitted. In its simplest form, it is simply a tag that other elements are nested within:
<form></form>
However, it can be modified with a number of attributes:
action The action
attribute specifies the url that this form data should be sent to. By default, it is the page the form exists on (i.e. if the form appears on http://foo.com/bar, then it will submit to http://foo.com/bar). The url can be relative to the current page, absolute, or even on a different webserver. See the discussion of URLs in the HTTP section.
enctype The enctype
attribute specifies the format that form data will be submitted in. The most common values are application/x-www-form-urlencoded
(the default), which serializes the key/value pairs using the urlencoding strategy, and multipart/form-data
, which uses the multipart encoding scheme, and can interweave binary (file) data into the submission. These encoding strategies are discussed more thoroughly in the chapter on submitting form data.
method The method
attribute specifies the HTTP method used to submit the form. The values are usually GET
or POST
. If the method is not specified, it will be a GET
request.
target The target attribute specifies how the server response to the form submission will be displayed. By default, it loads in the current frame (the _self
) value. A value of _blank
will load the response in a new tab. If <iframe>
elements are being used, there are additional values that work within <iframe>
sets.
Most inputs in a form are variations of the <input>
element, specified with the type
attribute. Many additional specific types were introduced in the HTML5 specification, and may not be available in older browsers (in which case, they will be rendered as a text type input). Currently available types are (an asterisk indicate a HTML5-defined type):
checked
, which is a boolean specifying if it is checked.In addition to the type attribute, some other commonly used input attributes are:
In addition to the <input>
element, some other elements exist that provide input-type functionality within a form, and implement the same attributes as an <input>
. These are:
The <textarea>
element provides a method for entering larger chunks of text than a <input type="text">
does. Most importantly, it preserves line-breaks (the <input type="text">
removes them). Instead of using the value
attribute, the current value appears inside the opening and closing tags, i.e.:
<textarea name="exampleText">
This text is displayed within the textarea
</textarea>
In addition, the rows
and cols
attribute can be used to specify the size of the textarea in characters.
The <select>
element allows you to define a drop-down list. It can contain as children, <option>
and <optgroup>
elements. The <select>
element should have its name
attribute specified, and each <option>
element should have a unique value
attribute. The selected <option>
’s value is then submitted with the <select>
’s name as a key/value pair.
Each <select>
element should also have a closing tag, and its child text is what is displayed to the user.
The <optgroup>
provides a way of nesting <option>
elements under a category identifier (a label
attribute specified on the <optgroup>
).
An example <select>
using these features is:
<select name="headgear">
<option value="none">None</option>
<optgroup label="Hats">
<option value="ball cap">Ball Cap</option>
<option value="derby">Derby</option>
<option value="fedora">Fedora</option>
</optgroup>
<optgroup value="Ceremonial">
<option value="crown">Crown</option>
<option value="mitre">Mitre</option>
<option value="war bonnet">War Bonnet</option>
</optgroup>
</select>
Finally, multiple selections can be allowed by specifying a multiple
attribute as true
.
In addition to inputs, a <form>
often uses <label>
elements to help identify the inputs and their function. A label will typically have its for
attribute set to match the name
attribute of the <input>
it corresponds to. When connected in this fashion, clicking the label will give focus to the input. Also, when the <input type="checkbox">
, clicking the label will also toggle the checked
attribute of the checkbox.
Finally, the <fieldset>
element can be used to organize controls and labels into a single subcontainer within the form. Much like <div>
elements, this can be used to apply specific styles to the contained elements.