Unfortunately when the HTML was being designed for forms 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) can be placed 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). Often, 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:
<form method="post" action="formhandler.php">
</form>
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 fieldset
s.
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).
The complete setup code for a form is therefore:
<form method="post" action="formhandler.php">
<fieldset>
<legend>Legend for form</legend>
<!-- form content goes here -->
</fieldset>
</form>
fieldset
and legend
are optional, but they are useful elements.
Photograph by Manfred Huszar, used under a Creative Commons Attribution-NonCommercial 2.0 Generic license
Enjoy this piece? I invite you to follow me at twitter.com/dudleystorey to learn more.