G.5. More Complex XHTML Forms

In the preceding section, we introduced basic forms. In this section, we introduce elements and attributes for creating more complex forms. Figure G.4 contains a form that solicits user feedback about a Web site.

Figure G.4. Form with text areas, a password box and checkboxes.

1 6 7 8

"http://www.w3.org/1999/xhtml"> 9 10 Internet and WWW How to Program - Forms 11 12 13 14 15

Feedback Form

16 17

Please fill out this form to help 18 us improve our site.

19 20 "post" action = "/cgi-bin/formmail"> 21 22

23 "hidden" name = "recipient" 24 value = "deitel@deitel.com" /> 25 "hidden" name = "subject" 26 value = "Feedback Form" /> 27 "hidden" name = "redirect" 28 value = "main.html" /> 29

30 31

Name: 32 "name" type = "text" size = "25" /> 33

34 35 36

Comments:

37 </span> <span style="color: #999999;"><span>"comments"</span></span> <span style="color: #000000;"><span>rows =</span></span> <span style="color: #999999;"><span>"4"</span></span> <span style="color: #000000;"><span>cols =</span></span> <span style="color: #999999;"><span>"36"</span></span><span style="color: #000000;"><span>></span></span></span> <span>38</span> <span style="background-color: #CCCCCC;">Enter your comments here. </span> <span>39</span> <span style="background-color: #CCCCCC;"> <span style="color: #000000;"><span> 40

41 42 43 44 45

E-mail Address: 46 "email" type = "password" 47 size = "25" /> 48

49 50

51 Things you liked:

52 53 Site design 54 "thingsliked" type = "checkbox" 55 value = "Design" /> 56 57 Links 58 "thingsliked" type = "checkbox" 59 value = "Links" /> 60

61 Ease of use 62 "thingsliked" type = "checkbox" 63 value = "Ease" /> 64 65 Images 66 "thingsliked" type = "checkbox" 67 value = "Images" /> 68 69 Source code 70 "thingsliked" type = "checkbox" 71 value = "Code" /> 72 73 74

75 "submit" value = 76 "Submit Your Entries" /> 77 "reset" value = 78 "Clear Your Entries" /> 79

80 81 82 83 84

The textarea element (lines 3739) inserts a multiline text box, called a text area, 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></span></span> and <span style="color: #000000;"><span><tt> tags. Default text can be specified in other input types, such as text boxes, by using the value attribute

The "password" input in lines 4647 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 characters that mask the input.

Lines 5471 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").

Common Programming Error G 2

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 them.

 

We continue our discussion of forms by presenting a third example that introduces several additional form elements from which users can make selections (Fig. G.5). In this example, we introduce two new input types. The first type is the radio button (lines 7694) 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. The radio buttons in a group all have the same name attributes and are distinguished by their different value attributes. The attribute-value pair checked = "checked" (line 77) indicates which radio button, if any, is selected initially. The checked attribute also applies to checkboxes.

Figure G.5. Form including radio buttons and a drop-down list.

(This item is displayed on pages 1505 - 1508 in the print version)

1 6 7 8

"http://www.w3.org/1999/xhtml"> 9 10 Internet and WWW How to Program - Forms 11 12 13 14 15

Feedback Form

16 17

Please fill out this form to help 18 us improve our site.

19 20 "post" action = "/cgi-bin/formmail"> 21 22

23 "hidden" name = "recipient" 24 value = "deitel@deitel.com" /> 25 "hidden" name = "subject" 26 value = "Feedback Form" /> 27 "hidden" name = "redirect" 28 value = "main.html" /> 29

30 31

Name: 32 "name" type = "text" size = "25" /> 33

34 35

Comments:

36 </span> <span style="color: #999999;"><span>"comments"</span></span> <span style="color: #000000;"><span>rows =</span></span> <span style="color: #999999;"><span>"4"</span></span> <span>37</span> <span style="color: #000000;"><span>cols =</span></span> <span style="color: #999999;"><span>"36"</span></span><span style="color: #000000;"><span>> 38

39 40

E-mail Address: 41 "email" type = "password" 42 size = "25" />

43 44

45 Things you liked:

46 47 Site design 48 "thingsliked" type = "checkbox" 49 value = "Design" /> 50 51 Links 52 "thingsliked" type = "checkbox" 53 value = "Links" />

54 55 Ease of use 56 "thingsliked" type = "checkbox" 57 value = "Ease" /> 58 59 Images 60 "thingsliked" type = "checkbox" 61 value = "Images" /> 62 63 Source code 64 "thingsliked" type = "checkbox" 65 value = "Code" /> 66 67 68 69 70 71 72

73 How did you get to our site?:

74 75 Search engine 76 "howtosite" type = "radio" 77 value = "search engine" checked = "checked" /> 78 79 80 Links from another site 81 "howtosite" type = "radio" 82 value = "link" /> 83 84 Deitel.com Web site 85 "howtosite" type = "radio" 86 value = "deitel.com" /> 87 88 Reference in a book 89 "howtosite" type = "radio" 90 value = "book" /> 91 92 Other 93 "howtosite" type = "radio" 94 value = "other" /> 95 96

97 98

99 Rate our site: 100 101 102 103 104 "rating"> 105"selected">Amazing 10610 [Page 1507]1079 1088 1097 1106 1115 1124 1133 1142 1151 116Awful 117 118 119 120

121 122

123 "submit" value = 124 "Submit Your Entries" /> 125 "reset" value = "Clear Your Entries" /> 126

127 128 129 130 131

Common Programming Error G 3

Not setting the name attributes of the radio buttons in a form to the same name is a logic error because it lets the user select all of them at the same time.

 

The select element (lines 104117) provides a drop-down list of items from which the user can select an item. The name attribute identifies the drop-down list. The option element (lines 105116) adds items to the drop-down list. The option element's selected attribute specifies which item initially is displayed as the selected item.

Категории