How to Create a Form

The various components of a form, and how to use them.

Please note that the form demos on this page will not actually send anywhere; their sole purpose is to illustrate the various parts of a form.

<form> | <input> | <textarea> | <select> | <option> | <optgroup> | <fieldset> | <legend> | <label>

The elements of a form

text boxes:

dropdown menu:
radio buttons:
checkboxes:
textarea:

 

The <form> tag

Every form begins with <form> and ends with </form>. Between those tags, there are four tags which define various types of form elements. Those tags are: <input>, <select>, <option>, and <textarea>.

The <form> tag itself has one required attribute, action="", which defines where the form contents will be sent when it has been submitted by the user. Most often, this destination is some kind of form processing script, located on the server. The <form> tag also takes the optional method="" and enctype="" attributes, in addition to other attributes used in many other HTML tags.

The method="" property can be either get (the default) or post. Using <form method="get"> adds the form contents to the browser's URL (useful for searches), while <form method="post"> hides the sent form data from the browser (used for sending email or sensitive information).

enctype="" specifies the MIME type used to encode the form data. It defaults to enctype="application/x-www-form-urlencoded". If the form is used to upload files, then this should be set to enctype="multipart/form-data".

The <input> tag

The <input> tag is used to define most of the form elements. It takes no separate closing tag. To conform to XHTML rules, it self-closes with /> at the end.

The type of form element is, appropriately enough, defined by the type="" attribute. The most commonly used <input> types (with examples of use) are:

Back to Top

<form> | <input> | <textarea> | <select> | <option> | <optgroup> | <fieldset> | <legend> | <label>

The <textarea> tag

Textarea demo

Your message:
 

The <textarea> tag creates a multi-row text box. It allows the form user to type a message into the form, which will be sent along with the rest of the form data.

The width and height of the box can be controlled by setting the attributes, cols="" (number of columns wide) and rows="" (number of rows high). As with the single line textbox, you may also include maxlength="" to limit the number of characters it will accept. But make sure your form processing script also sets a character limit on the server, to prevent someone from submitting anything other than a friendly text message.

If you would like your textarea to already contain some text for the user to read, type it between the opening and closing <textarea> tags.


<form action="ExAcT_path_to_your_form_processor" method="">
 Your message:<br />
 <textarea cols="24" cols="5" name="comment">optional pretyped text</textarea>
</form>


Back to Top

<form> | <input> | <textarea> | <select> | <option> | <optgroup> | <fieldset> | <legend> | <label>

The <select> and <option> tags

Select one Letter from A to E:

Select any Letters from F to J:

   

The <select> tag is used together with the <option> tag, to choose one or more items from a listbox or dropdown list.

By default, only one option is displayed at a time, and only one option can be selected at a time. To allow multiple selections, the <select> tag can include a size="" attribute, and a multiple="multiple", which defines how many options display at once, and allows the user to select more than one at a time. Setting size="1" (the default) produces a dropdown box, and any amount greater than 1 produces a list box.

An option may be pre-selected by placing selected="selected" inside the appropriate <option> tag.

In the A to E dropdown menu, the value="" of the first option is left empty, to create a header line. In the "F to J" dropdown menu, the empty option has been omitted. You can choose whichever way works best for you.

You can have as many options as you want inside each <select> tag. If you use more than one <select> tag, each one must have a unique name. As with the <input> tag above, a name may not contain spaces.

Example of use:


<form action="ExAcT_path_to_your_form_processor" method="">

 <p>Select one Letter from A to E:</p>

 <select name="AtoE" title="choose from A to E"> <!-- this is a dropdown -->
  <option value="">choose from A to E</option> <!-- this line is optional -->
  <option value="A">A</option>
  <option value="B">B</option>
  <option value="C">C</option>
  <option value="D">D</option>
  <option> value="E">E</option>
 </select>

 <p>Select any Letters from F to J:</p>

 <select name="FtoJ" size="5" multiple="multiple" title="choose any from F to J"> <!-- this is a listbox -->
  <option value="F">F</option>
  <option value="G">G</option>
  <option value="H" selected="selected">H</option>
  <option value="I">I</option>
  <option value="J">J</option>
 </select>
</form>


Back to Top

<form> | <input> | <textarea> | <select> | <option> | <optgroup> | <fieldset> | <legend> | <label>

The <optgroup> tag

Select a Shape:

 

The <optgroup> tag can be used in dropdown and list boxes to contain a group of <option> tags. It helps to visually and logically group similar options in a dropdown list together. It takes the attribute, label="", which can contain header text to label each group. It also requires a closing </optgroup> tag. Use it to surround a group of <option> tags. Example coding:


<form action="ExAcT_path_to_your_form_processor" method="">

 Select a Shape:<br /><br />

 <select name="shapes">
 <option value="">2D or 3D</option> <!-- this line is optional -->
 <optgroup label="2D">
  <option value="circle">circle</option>
  <option value="triangle">triangle</option>
  <option value="square">square</option>
 </optgroup>

 <optgroup label="3D">
  <option value="sphere">sphere</option>
  <option value="pyramid">pyramid</option>
  <option value="cube">cube</option>
 </optgroup>
 </select>

</form>


Back to Top

<form> | <input> | <textarea> | <select> | <option> | <optgroup> | <fieldset> | <legend> | <label>

The <fieldset> and <legend> tags

Schools


Work Places

 

The fieldset tag is used to group several related form elements together. Some browsers will visually show the groupings, while others will read the form elements aloud. The fieldset also facilitates tabbed navigation of a document. You can add a caption to a fieldset tag with the legend tag, which some screen readers can read aloud as well. While the legend tag not widely supported by current browsers, it must be placed directly after fieldset, before any other elements.


<fieldset>
<legend>
Schools</legend>
<label for="college">college</label>
<input type="text" name="college"
id="college" /><br />

<label for="university">university</label>
<input type="text" name="university"
id="university" /><br />

<label for="hardknocks">hard knocks</label>
<input type="text" name="hardknocks"
id="hardknocks" /><br />
</fieldset>

<fieldset>
<legend>
Work Places</legend>
<label for="office">office</label>
<input type="text" name="office"
id="office" /><br />

<label for="home">home</label>
<input type="text" name="home"
id="home" /><br />

<label for="field">field</label>
<input type="text" name="field"
id="field" />
</fieldset>

Back to Top

<form> | <input> | <textarea> | <select> | <option> | <optgroup> | <fieldset> | <legend> | <label>

The <label> tag


 

The <label> tag can be used to label any input, textarea, or select form element. The optional for attribute can be used to associate the label with a particular element, by making its value the same as the element's id. Labels make a form more accessible, because they can be read by speech synthesizers, and because they can make the associated form field clickable in a visual browser.

Coding:

<label for="fieldone">Field 1</label>
<input type="text" name="fieldone" id="fieldone" /><br />

<label for="fieldtwo">Field 2</label>
<input type="text" name="fieldtwo" id="fieldtwo" />

And there you have it. Those are the basics of form coding.
Coming soon:  Making the form work

<form> | <input> | <textarea> | <select> | <option> | <optgroup> | <fieldset> | <legend> | <label>

Back to Top