demosthenes.info

A blog by Dudley Storey on , , , , , , and anything else that strikes his fancy.

featured articles

popular favourites

The Form Tag

Unfortunately when the HTML for forms was being designed consistency was not a high priority. You will find that most form-specific tags are <input> tags, but there are several notable exceptions.

The main points to remember about forms are as follows:

  • a form starts with a <form> tag and closes with a matching </form> tag. Form elements (text fields, drop-down menus, etc) are only valid between the opening and closing tag in XHTML. In HTML5 you may put a form element, such as an input, anywhere on the page, with or without a surrounding form tag.

  • The form tag has two attributes that are typically added: method and action.

  • A form’s method is the way the submitted data is passed to whatever will be processing it (a CGI script, a PHP script, etc). Ninety percent of the time this method will be post. Alternatively, the get method appends the data to the URL itself when it is submitted, which is obviously less secure (but helpful, if you are interested in debugging a form or bookmarking a page created through a form decision).

  • The action attribute of the form tag tells the form where to submit its data to. If a CGI script written in Perl was processing the data, the action attribute would typically be set to something like cgi-bin/form.pl. If a PHP script, it would be something like formhandler.php.

The first step in creating our form is the opening and closing <form> tags, with the correct attributes:

  1. <form method="post" action="formhandler.php">
  2. </form>

XHTML requires a fieldset tag immediately inside the form tag. fieldset is a container element inside the form. Think of filling out your tax form at the end of each year: each major section of the form (personal information, employer, income) is divided into a seperate box. fieldset is that box in the context of a form. A form may contain multiple fieldsets.

Immediately inside the fieldset you should enter a <legend> tag, with appropriate content. The <legend> is essentially the label for the entire fieldset, and typically indicates the purpose of that section. (“Personal information”, “Mailing Address”, “Billing Info”, etc).

fieldset is optional in HTML5, but it is a good idea to use it.

web developer guide

featured comment

by JoelB in Goodbye, JQuery Validation: HTML5 Form Errors With CSS3

what i'm reading

A Storm of Swords: A Song of Ice and Fire: Book Three
A Storm of Swords: A Song of Ice and Fire: Book Three

what i'm watching

Californication: The Third Season
Californication: The Third Season

what i'm playing

Mass Effect 3 Collector's Edition
Mass Effect 3 Collector's Edition

what i'm hearing

Dub FX
Dub FX

blogs

podcasts

no ads ever

This blog is free of advertising, and always will be.

creative commons licensed

The content of this blog is free to use in whatever way you wish under the Creative Commons license.