J.13. More Complex XHTML Forms
In the previous section, we introduced basic forms. In this section, we introduce elements and attributes for creating more complex forms. Figure J.13 contains a form that solicits user feedback about a Web site.
Figure J.13. Form with textareas, password boxes and checkboxes.
(This item is displayed on pages 1352 - 1354 in the print version)
"http://www.w3.org/1999/xhtml"> 9 10
The textarea element (lines 4244) inserts a multiline text box, called a textarea, into the form. The number of rows is specified with the rows attribute and the number of columns (i.e., characters) is specified with the cols attribute. In this example, the textarea is four rows high and 36 characters wide. To display default text in the text area, place the text between the </tt> and <tt> tags. Default text can be specified in other input types, such as textboxes, by using the value attribute.
The "password" input in lines 5253 inserts a password box with the specified size. A password box allows users to enter sensitive information, such as credit card numbers and passwords, by "masking" the information input with asterisks. The actual value input is sent to the Web server, not the asterisks that mask the input.
Lines 6078 introduce the checkbox form element. Checkboxes enable users to select from a set of options. When a user selects a checkbox, a check mark appears in the check box. Otherwise, the checkbox remains empty. Each "checkbox" input creates a new checkbox. Checkboxes can be used individually or in groups. Checkboxes that belong to a group are assigned the same name (in this case, "thingsliked").
We continue our discussion of forms by presenting a third example that introduces several more form elements from which users can make selections (Fig. J.14). In this example, we introduce two new input types. The first type is the radio button (lines 90113), specified with type "radio". Radio buttons are similar to checkboxes, except that only one radio button in a group of radio buttons may be selected at any time. All radio buttons in a group have the same name attribute; they are distinguished by their different value attributes. The attributevalue pair checked ="checked" (line 92) indicates which radio button, if any, is selected initially. The checked attribute also applies to checkboxes.
Figure J.14. Form including radio buttons and drop-down lists.
(This item is displayed on pages 1355 - 1358 in the print version)
"http://www.w3.org/1999/xhtml"> 9 10
Common Programming Error J.7
When your form has several checkboxes with the same name, you must make sure that they have different values, or the scripts running on the Web server will not be able to distinguish between them. |
Common Programming Error J.8
When using a group of radio buttons in a form, forgetting to set the name attributes to the same name is a logic error that lets the user select all of the radio buttons at the same time. |
The select element (lines 123136) provides a drop-down list from which the user can select an item. The name attribute identifies the drop-down list. The option element (lines 124135) adds items to the drop-down list. The option element's selected attribute specifies which item initially is displayed as the selected item in the select element.