Versión en español




Tutorial: Forms in HTML


Forms are a characteristic of the HTML standard that let authors collect information provided by the visitors. This forms can be useful to collect personal information, contact information, preferences or opinions, or any kind of user input the author may need. In this tutorial we'll explore all the characteristics available to build forms in HTML.

A form can be inserted into an HTML document through the HTML form tag which acts as a container for all the input elements. All the information gathered by a form can be submitted to a processing agent that's usually specified in the "action" attribute (which can be overridden using Javascript). What the processing agent does with the information and how it handles it will not be treated in this site given that it doesn't belong to the HTML standard. To handle forms' data you must use a server side script.

You may also need to specify how the data will be sent in the value of the "method" attribute: "post" (the form's data is attached to the body of the form) or "get" (the form's data is attached to the URL). The processing agent is supposed to know and handle the submit method of the form.

This way, a simple form can have the next declaration:

Code begin<form method="post" action="handler.php">
...Controls...
</form>
Code end

Input elements

Most of the input controls are visual and can interact with the user. Their use depends on the type of control and also the kind of information they can collect. The input elements of a form can be defined using these tags: the HTML input tag, the HTML button tag, the HTML select tag and the HTML textarea tag. In this tutorial we'll divide the controls by their functionality. Note: as the descriptions and attributes for each control are briefly treated in this tutorial, please refer to these tags references for more information.

Obrserve that the "name" attribute of each control will be the name used to identify the data for the processing agent, and the value will depend on the control's nature (sometimes, like in check boxes or radio buttons, will be the content of the "value" attribute).

Text inputs

There are three types of text inputs that can collect textual information like names, comments, etc.

Options

Authors can also let their visitors pick up preestablished options from a list. This can be achieved using one of the three following controls, that can build different types of option lists.

Buttons

Buttons are useful in forms to submit it, reset it or to execute custom functions. They can be inserted using the HTML input tag (submit, reset and image) or the HTML button tag (content buttons).

File input

File inputs can be used to upload files to the server. The control shows a text box where the user must specify the location of the file (that will be processed locally by the browser) that will be sent to the server. This way authors can request visitors to send files through the page. The control usually shows a button to pick up the file visually.

Note that for forms with file uploads you must specify the value "multipart/form-data" in the "enctype" attribute of the HTML form tag.

Code View
<form method="post" action="handler.php" enctype="multipart/form-data">
<p><input name="image" type="file" /></p>
</form>

Labeling elements

Element's labels can make your page look better and add a small functionality for visual user agents (when a visitor clicks the label the action is passed to its associated control), but they will certainly make a lot of sense for people with disabilities or non-visual browsers. A label establishes a relationship between a control and a piece of text (that's supposed to set a "title" for the control).

Labels can be inserted using the HTML label tag and are associated to controls through the "for" attribute. To do so, the value of the "for" attribute from the HTML label tag must match the value of the "id" attribute from the control.

Code View
<form method="post" action="handler.php"><div>
<label for="idfname">First name:</label> <input type="text" id="idfname" name="fname" /><br />
<label for="idlname">Last name:</label> <input type="text" id="idlname" name="lname" /><br /><br />
Gender:<br /><input type="radio" id="idmale" name="gender" /><label for="idmale">Male</label><br />
<input type="radio" id="idfemale" name="gender" /><label for="idfemale">Female</label>
</div></form>



Gender:

Grouping elements

All elements in a form can also be grouped thematically with the HTML fieldset tag. This tag contains a group of controls that are related to each other for some reason (e.g. personal information, work information, financial information, interests, etc.).

The caption for each fieldset can be set with the HTML legend tag that must be defined right after the fieldset start tag, and give a descriptive title for the group of controls.

Code View
<form method="post" action="handler.php">
<fieldset>
<legend>Personal information</legend>
First name: <input type="text" name="fname" /><br />
Last name: <input type="text" name="lname" /><br />
Address: <input type="text" name="paddres" /><br />
Phone: <input type="text" name="pphone" />
</fieldset>
<fieldset>
<legend>Work information</legend>
Address: <input type="text" name="waddress" /><br />
Phone: <input type="text" name="wphone" />
</fieldset>
</form>
Personal information First name:
Last name:
Address:
Phone:
Work information Address:
Phone:

The use of this grouping is wide and depends on each specific form, but can be very helpful for visitors when filling large forms (specially for non-visual user agents).


Next tutorial: XHTML code >>


Bypass footer options  |   Send to a friend Send to a friend  |  Post to del.icio.us Post to del.icio.us

Digg this page Digg this!  |  File on Furl File on Furl  |  Add to Yahoo! MyWeb Add to Yahoo! MyWeb

Bypass W3C declarations | 

Valid XHTML 1.0 Strict  |  Valid CSS Why should you trust us? Click the images on the left to see how seriously we write our own pages, then make your choice.

Level Triple-A conformance icon, W3C-WAI Web Content Accessibility Guidelines 1.0. This website gives its best effort to achieve the Level Triple-A Conformance, W3C-WAI Web Content Accessibility Guidelines 1.0. If you find any detail or error that we didn't see, don't hesitate and let us know.

The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect. -- Tim Berners-Lee, W3C Director and inventor of the World Wide Web.

 Link to us  |  Contact us  |  Beyond HTML  |  Tools and resources  |  Sitemap  |  Webmaster