Larger Text Normal Text Smaller Text

IT 280 Web Development II, Spring 2008

 

Forms in XHTML

Overview

A form in XHTML is just a user interface for entering data. It is the processing that makes it useful.

Components

Forms have two parts:

Form Controls

Syntax

A form is a container element. The general structure of a basic form is:

        <form action="URL
                            other attributes"
        >

            <input  type="type"
                   name="name"
                  other attributes />
                  
        </form>
  

In XHTML, the action attribute is required.

Example 1

Here's an example: (more about the action later.)

Basic form

    <form action="/cgi-bin/echo_form.cgi">
          <input type="text" 
                       name="demo" 
                       value="Initial text" 
          />
    </form>
  

It produces the rendered input element:

The type determines the appearance and handling of the control produced by the browser.

The final look is determined by the browser.

The example in a separate page.

Example 2

Here is a form with four different types of input elements, as rendered by the client software you are currently using:

Form showing different types of input elements

I am: Undergrad
Grad

I like: Surfing the Web
Making my own Pages

The example as a separate page.

The same form as seen in a line mode browser.

Element summary

Link to a chart showing the tags for different form controls.

Form actions

Formcontrols are essentially a combination of string variables and a method of entering values for them; the browser renders the fields and manages the input values. But what can we do with them?

The input element with type="submit" tells the browser to carry out the specified action URL.

The action is similar to the href of an anchor element; it can be of several different kinds, including:

  1. Send the data as part of an HTTP request.
  2. Process the data using a client-side script such as JavaScript. (If supported)
  3. Send the data as an email message. (If supported)

Controlling form display

Users need to know what information the form controls are asking for. One common, but not always effective, approach is to use tables to visually associate identifying text with controls.

MISSING IMAGE

However, the the association is purely visual and the result may not be as effective with a line-mode browser, screenreader, or other type of user agent.

MISSING IMAGE

The next several elements were added in XHTML to improve the accessibility and presentation of forms.

label

The label inline element can logically associate text with form controls.

Example 3: Labels

Two ways of associating a label with a control

    <form action="/cgi-bin/echo_form.cgi">
      
          <label>First element 
          <input type="text" 
              		   name="demo"
              		   id="d1" 
              		   value="Initial text" />
          </label>
          
          <label for="d2">Second element</label> 
          <input type="text" 
          		       name="demotoo"
          		       id="d2"
          		       value="More Initial text" />

    </form> 

As you can see, here and in The example as a separate page., a typical graphic browser, such as IE or Firefox doesn't give the label any special treatment. However it is available for use by non-graphic browsers if needed.

fieldset and legend elements

Blocks of form elements can be grouped using fieldset. The fieldset block may include an optional legend element. User agents may apply special rules to the group of elements such as distinctive styling and keeping the fieldset text on the same page when printing, if possible.

Example 4: fieldset & legend

Fieldset with and without a legend

 
    <form action="/cgi-bin/echo_form.cgi">
      
      <fieldset>
          <legend>It's legendary!</legend>
              <input type="text" 
              		 name="demo" 
              		 value="Initial text" />
      </fieldset>
      
      <fieldset>
          <input type="text" 
          		 name="demotoo"
          		 id="d2"
          		 value="More Initial text" />
      </fieldset>   		 
    </form>  
It's legendary!

In the example as a separate page, the fieldset elements have some additional styling.

Selection list options

Options in a selection list may be grouped using optgroup. This provides additional flexibility for different user agents.

The option element also has an optional label attribute which is not the same as the label element. The label attribute provides alternative text that may be used in the selection list by some user agents, such as shorter text to used by handheld devices.

Warning:

The attribute multiple for the select element indicates that multiple options may be selected. It is often displayed as a list with scrollbars.

Example 5: Selection list options

optgroup and option labels

      <select name="entered" id="entered">
        <optgroup label="This year.">
            <option label="2007" value="07" selected="selected">Current: 2007</option>
        </optgroup>         
        <optgroup label="Recent">
            <option label="2006" value="06">Recent: 2006</option>
            <option label="2005" value="05">Recent: 2005</option>
            <option label="2004" value="04">Recent: 2004</option>  
        <optgroup label="Not-so-recent">
            <option label="2003" value="03">A while ago: 2003</option>
            <option label="2002" value="02">A while ago: 2002</option>
            <option label="2001" value="01">A while ago: 2001</option>
            <option label="2000" value="00">A while ago: 2000</option>
        </optgroup>
            <option label="Pre Y2K" value="oldie">Ages ago: 1999 or earlier</option>
        </select>
        

In the example as a separate page, the select element has a few more options.

Example 6

Example form illustrating all these elements.

The Source code for the example form.

Form components summary

form: contains all other form elements
fieldset     legend
label     input (textboxes, radio buttons, checkboxes, submit & reset)
    textarea
    button
select
optgroup
option

Note: It is not really recommended to use fieldset for this sort of visual display trick that is unrelated to the definition of the element.

Navigating forms: the tabindex and accesskey attributes

Non-graphic browsers use the keyboard or other input for navigation, not a mouse.

Users of graphical browsers may use the keyboard or voice for navigation, either by necessity or preference.

In long documents, it's good if the user doesn't have to tab through all of the links or form elements to reach one specific one.

This is easy enough with the mouse, but tabbing is a sequential access.

The specific effect of the key depends on the associated element.

Example 3

The following example illustrates both attributes. Experiment with tabbing and with the access keys (the bracketed numerals).

The highlighting is created with the CSS :focus pseudo-class.

Tabbing index and access keys

<form id="showoff" action="">
  <div class="showy">

    <label accesskey="1">One [1] 
          <input type="text" tabindex="1" /></label><br />
    <a accesskey="4" href="/" tabindex="4">Four [4]</a>

          wyrd index<br/>

    <label accesskey="3">Three [3] 
          <input type="checkbox" tabindex="3" /></label><br />
    <a accesskey="6" href="forms.shtml" tabindex="6">Six [6]</a> 
          top of page<br/>

    <label accesskey="2">Two [2] 
          <input type="text" tabindex="2" /></label><br />

    <a accesskey="7" href="./" tabindex="7">Seven [7]</a>
          class index<br/>
    <label accesskey="5">Five[5] 
          <input type="text" tabindex="5" /></label><br />



Four [4] wyrd index

Six [6] top of page

Seven [7] class index

The example as a separate page.

Designing forms

Guidelines from Sensible Forms: A Form Usability Checklist by Brian Crescimanno

Lab Exercise: Create an online form

Create a basic HTML feedback form for your site. It should be designed to provide information to you about how visitors .

TEST THE FORM by coding it with an ACTION that submits the form to http://wyrd.hood.edu/cgi-bin/echo_form.cgi so that you can see exactly what is being received by the program. (This is very useful when developing and debugging a form.)

Your form should include at least the following form elements:

You can ask different questions as long as you use the required elements.

Overall, the exercise page should: