1. Trang chủ
  2. » Công Nghệ Thông Tin

html xhtml and css for dummies 7th edition phần 7 pdf

41 478 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 41
Dung lượng 2,32 MB

Nội dung

226 Part IV: Scripting and (X)HTML Webmonkey offers a good overview of the difference between get and post in its “Add HTML Forms to Your Site” article at www.webmonkey. com/2010/02/add_html_forms_to_your_site. Markup The markup in Listing 14-1 creates a form that uses the post method to send user-entered information to a form handler (guestbook.php) to be pro- cessed on the Web server. Listing 14-1: A Simple Form Processed by a Form Handler <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”> <html xmlns=”http://www.w3.org/1999/xhtml”> <head> <title> Forms </title> <meta http-equiv=”Content-Type” content=”text/html; charset=ISO-8859-1” /> </head> <body> <form action=”cgi-bin/guestbook.php” method=”post”> <! form input elements go here > </form> </body> </html> The value of the action attribute is a URL, so you can use absolute or rela- tive URLs to point to a form handler on your server. Absolute and relative URLs are covered in more detail in Chapter 6. Input tags The tags you use to solicit input from your site visitors make up the bulk of any form. HTML supports a variety of different input options — from text fields to radio buttons and from files to images. Every input control associates some value with a name: ✓ When you create the control, you give it a name. ✓ The control sends back a value based on what the user does in the form. For example, if you create a text field that collects a user’s first name, you might name the field firstname. When the user types her first name in the field and submits the form, the value associated with firstname is whatever name the user typed in the field. 22_9780470916599-ch14.indd 22622_9780470916599-ch14.indd 226 11/30/10 12:26 AM11/30/10 12:26 AM 227 Chapter 14: Working with Forms The whole point of a form is to gather values associated with input controls, so how you set the name and value for each control is important. The follow- ing sections explain how you should work with names and values for each of the input controls. The input element (and by extension, the empty <input … /> tag) is the major player when it comes to using HTML forms to solicit user input. Inside the input element is where you define the kinds of input you want to collect, and how you package and present the input fields and cues you present to users so they can give you what you’re asking for. Input fields You can use a variety of input fields in your forms, such as text, password, radio buttons/check boxes, hidden, and more. Not all fields require values for name and type attributes (for example, text box or password fields), but it’s a good idea to provide users with explanatory labels and examples of input data any time they might have questions about formats — as when ponder- ing whether or not to include dashes or spaces in credit card or telephone numbers. Check boxes and radio buttons, on the other hand, require such information so they can be properly labeled when the browser shows users what selections are available. For input elements that require a user to select an option (a check box or radio button) rather than typing something into a field, you define both the name and the value. When the user selects a check box or a radio button and then clicks the Submit button, the form returns the name and value assigned to the element. We discuss these two types of input fields in the upcoming section, “Check boxes and radio buttons.” Text fields Text fields are single-line fields in which users type information. When you need to offer the user the opportunity to fill in more than one line, you use a text box, as we discuss in the upcoming section, “Multiline text boxes.” Here’s how to create a single-line text field: 1. Define the input type as a text field by using the <input /> element with the type attribute set to text. <input type=”text” /> 2. Then use the name attribute to give the input field a name. <input type=”text” name=”firstname” /> The user supplies the value when she types in the field. 22_9780470916599-ch14.indd 22722_9780470916599-ch14.indd 227 11/30/10 12:26 AM11/30/10 12:26 AM 228 Part IV: Scripting and (X)HTML The following markup creates two text input fields — one for a first name and one for a last name: <form action=”cgi-bin/guestbook.php” method=”post”> <ul style= ” list-style-type: none; ” > <li> First Name: <input type=”text” name=”firstname” /></li> <li> Last Name: <input type=”text” name=”lastname” /></li> </ul> </form> In addition to the <input /> elements, the preceding markup includes list (<ul> and <li>) elements and some text to label each of the fields. By them- selves, most form elements don’t give the user many clues about the type of information you want them to enter. Lists are covered in more detail in Chapter 5. You must use HTML block and inline elements to format the appearance of your form and also to supply the necessary text. Figure 14-5 shows how a browser displays this kind of HTML. (To see the HTML source that produced this figure, visit our Web site at www.dummieshtml.com, pick Chapter 14, and look at the source code for Figure 14-5.) Figure 14-5: Text entry fields in a form. You can control the size of a text field with these attributes: ✓ size: The length (in characters) of the text field ✓ maxlength: The maximum number of characters the user can type into the field The following markup creates a form that sets both fields to a size of 30 (characters long) and a maxlength of 25 (characters long). Even though each field will be about 30 characters long, a user can type only 25 characters into each field, as shown in Figure 14-6. (Setting the size attribute greater 22_9780470916599-ch14.indd 22822_9780470916599-ch14.indd 228 11/30/10 12:26 AM11/30/10 12:26 AM 229 Chapter 14: Working with Forms than maxlength ensures that the text field will always have some white space between the user input and the end of the field box on display; you don’t have to do this yourself, but we find it visually pleasing.) <form action=”cgi-bin/guestbook.php” method=”post”> <ul style= ” list-style-type: none; ” > <li> First Name: <input type=”text” name=”firstname” size= ” 30 ” maxlength= ” 25 ” /></li> <li> Last Name: <input type=”text” name=”lastname” size= ” 30 ” maxlength= ” 25 ” /></li> </ul> </form> Figure 14-6: You can specify the length and maximum number of characters for a text field. Password fields A password field is a special text field that doesn’t display what the user types. Each keystroke is represented on the screen by a placeholder char- acter, such as an asterisk or bullet, so that someone looking over the user’s shoulder can’t see sensitive information. You create a password field by using the <input /> element with the type attribute set to password, as follows: <form action=”cgi-bin/guestbook.php” method=”post”> <ul style= ” list-style-type: none; ” > <li> First Name: <input type=”text” name=”firstname” size= ” 30 ” maxlength= ” 25 ” /></li> <li> Last Name: <input type=”text” name=”lastname” size= ” 30 ” maxlength= ” 25 ” /></li> <li> Password: <input type=”password” name=”psswd” size=”30” maxlength=”25” /></li> </ul> </form> 22_9780470916599-ch14.indd 22922_9780470916599-ch14.indd 229 11/30/10 12:26 AM11/30/10 12:26 AM Downloa d f r o m W o w ! e B o o k < w w w.wowebook.com> 230 Part IV: Scripting and (X)HTML Password fields are programmed like text fields. Figure 14-7 shows how a browser replaces what you type with bullets. Note: Depending on the browser’s default settings, some browsers will replace the text with asterisks or some other character. Figure 14-7: Password fields mask the text a user enters. Check boxes and radio buttons If only a finite set of possible values is available to the user, you can give him a collection of options to choose from: ✓ Check boxes: Choose more than one option. ✓ Radio buttons: Choose only one option. Radio buttons differ from check boxes in an important way: Users can select a single radio button from a set of options but can select any number of check boxes (including none, one, or more than one). If many choices are available (more than half-a-dozen or so), use a drop-down list instead of radio buttons or check boxes. We show you how to create those in the upcoming section, “Drop-down list fields.” To create radio buttons and check boxes, you 1. Use the <input /> element with the type attribute set to radio or checkbox. 2. Create each option with these attributes: • name: Give the option a name. • value: Specify what value is returned if the user selects the option. 22_9780470916599-ch14.indd 23022_9780470916599-ch14.indd 230 11/30/10 12:26 AM11/30/10 12:26 AM 231 Chapter 14: Working with Forms You can also use the checked attribute (with a value of checked) to specify that an option should be already selected when the browser displays the form. This is a good way to specify a default selection in a list. The following markup shows how to format check box and radio button options: <form action=”cgi-bin/guestbook.cgi” method=”post”> <p> What are some of your favorite foods ?</p> <ul style= ” list-style-type: none; ” > <li><input type=”checkbox” name=”food” value=”pizza” checked=”checked” /> Pizza </li> <li><input type=”checkbox” name=”food” value=”icecream” /> Ice Cream </li> <li><input type=”checkbox” name=”food” value=”eggsham” /> Green Eggs and Ham </li> </ul> <p> What is your gender? </p> <ul style= ” list-style-type: none; ” > <li><input type=”radio” name=”gender” value=”male” /> Male </li> <li><input type=”radio” name=”gender” value=”female” checked=”checked” /> Female </li> </ul> </form> In the preceding code, each set of options uses the same name for each input control but gives a different value to each option. You give each item in a set of options the same name to let the browser know they’re part of a set. Figure 14-8 shows how a browser displays this markup, where we also checked the box for Pizza and left the default check next to Ice Cream as-is. If you want to, in fact, you can check as many boxes as you like by default in the page markup, simply by including checked=”checked” in each <input … /> element you choose to check in advance. Hidden fields A hidden field gives you a way to collect name and value information that the user can’t see along with the rest of the form data. Hidden fields are useful for keeping track of information associated with the form, such as its version or name. If your Internet service provider (ISP) provides a generic application for a guest book or feedback form, you might have to put your name and e-mail address in the form’s hidden fields so that the data goes specifically to you. 22_9780470916599-ch14.indd 23122_9780470916599-ch14.indd 231 11/30/10 12:26 AM11/30/10 12:26 AM 232 Part IV: Scripting and (X)HTML Figure 14-8: Check boxes and radio buttons offer choices. To create a hidden field, you ✓ Use the <input /> element with its type attribute set to hidden. ✓ Supply the name and value pair you want to send to the form handler. Here’s an example of markup for a hidden field: <form action=”cgi-bin/guestbook.php” method=”post”> <input type=”hidden” name=”e-mail” value=”me@mysite.com” /> <ul style= ” list-style-type: none; ” > <li> First Name: <input type=”text” name=”firstname” size= ” 30 ” maxlength= ” 25 ” /></li> <li> Last Name: <input type=”text” name=”lastname” size= ” 30 ” maxlength= ” 25 ” /></li> <li> Password: <input type=”password” name=”psswd” size=”30” maxlength=”25” /></li> </ul> </form> As a general rule, using your e-mail address in a hidden field is just asking for your address to be picked up by spammers. If your ISP says that this is how you should do your feedback form, ask for suggestions as to how you can minimize the damage. Surfers to your page can’t see your e-mail address, but spammers’ spiders can read the underlying tags. At a minimum, you would hope that your ISP supports one of the many JavaScript encryption tools available to obscure e-mail addresses from harvesters. 22_9780470916599-ch14.indd 23222_9780470916599-ch14.indd 232 11/30/10 12:26 AM11/30/10 12:26 AM 233 Chapter 14: Working with Forms File upload fields A form can receive documents and other files, such as images, from users. When the user submits the form, the browser grabs a copy of the file and sends it with the other form data. To create this file upload field ✓ Use the <input /> element with the type attribute set to file. The file itself is the form field value. ✓ Use the name attribute to give the control a name. Here’s an example of markup for a file upload field: <form action=”cgi-bin/guestbook.php” method=”post”> <p> Please submit your resume in Microsoft Word or plain text format: <br /> <input type=”file” name=”resume” /> </p> </form> Browsers render a file upload field with a Browse button that allows a user to navigate a local hard drive and select a file to send, as shown in Figure 14-9. Figure 14-9: A file upload field. 22_9780470916599-ch14.indd 23322_9780470916599-ch14.indd 233 11/30/10 12:26 AM11/30/10 12:26 AM 234 Part IV: Scripting and (X)HTML When you accept users’ files through a form, you may receive files that are either huge or perhaps virus-infected. Consult with whomever is program- ming your form handler to discuss options to protect the system where files get saved. Several barriers can help minimize your risks, including ✓ Virus-scanning software ✓ Restrictions on file size ✓ Restrictions on file type Drop-down list fields Drop-down lists are a great way to give users lots of options in a small amount of screen space. You use two different tags to create a drop-down list: ✓ <select> creates the list. Use a name attribute with the <select> element to name your list. ✓ A collection of <option> elements identifies individual list options. The value attribute assigns a unique value for each <option> element. Here’s a markup example for a drop-down list: <form action=”cgi-bin/guestbook.cgi” method=”post”> <p> What is your favorite food? </p> <select name=”food”> <option value=”pizza”> Pizza </option> <option value=”icecream”> Ice Cream </option> <option value=”eggsham”> Green Eggs and Ham </option> </select> </form> The browser turns this markup into a drop-down list with three items, as shown in Figure 14-10. You can also enable users to select more than one item from a drop-down list by changing the default settings of your list: ✓ If you want your users to be able to choose more than one option (by holding down the Ctrl [Windows] or Ô [Mac] key while clicking options in the list), add the multiple attribute to the <select> tag. The value of multiple is multiple. Because of XHTML rules, standalone attributes cannot stand alone; therefore, the value is the same as the name for the attribute. 22_9780470916599-ch14.indd 23422_9780470916599-ch14.indd 234 11/30/10 12:26 AM11/30/10 12:26 AM 235 Chapter 14: Working with Forms ✓ By default, the browser displays only one option until the user clicks the drop-down menu arrow to display the rest of the list. Use the size attri- bute with the <select> tag to specify how many options to show. If you specify fewer than the total number of options, the browser includes a scroll bar with the drop-down list. Figure 14-10: A drop-down list. You can specify that one of the options in the drop-down list be already selected when the browser loads the page, just as you can specify a check box or radio button to be checked. Simply add the selected attribute to have a value of selected for the <option> tag you want as the default. Use this when one choice is very likely, but don’t worry — users can override your default selection quickly and easily. The following markup example ✓ Allows the user to choose more than one option from the list ✓ Displays two options ✓ Selects the third option in the list by default <form action=”cgi-bin/guestbook.cgi” method=”post”> <p> What are some of your favorite foods? </p> <select name=”food” size=”2” multiple=”multiple”> <option value=”pizza”> Pizza </option> <option value=”icecream”> Ice Cream </option> <option value=”eggsham” selected=”selected”> Green Eggs and Ham </option> </select> </form> 22_9780470916599-ch14.indd 23522_9780470916599-ch14.indd 235 11/30/10 12:26 AM11/30/10 12:26 AM [...]... 22_ 978 0 470 916599-ch14.indd 2 37 11/30/10 12:26 AM 238 Part IV: Scripting and (X )HTML Listing 14-2: A Complete Multi-Part Form Basic Form Markup . Multi-Part Form <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR /xhtml1 /DTD /xhtml1 -transitional.dtd”> < ;html xmlns=”http://www.w3.org/1999 /xhtml lang=”en”. multiple. Because of XHTML rules, standalone attributes cannot stand alone; therefore, the value is the same as the name for the attribute. 22_ 978 0 470 916599-ch14.indd 23422_ 978 0 470 916599-ch14.indd. Submit and Reset buttons named Send and Clear, respectively: 22_ 978 0 470 916599-ch14.indd 2 372 2_ 978 0 470 916599-ch14.indd 2 37 11/30/10 12:26 AM11/30/10 12:26 AM 238 Part IV: Scripting and (X)HTML

Ngày đăng: 14/08/2014, 12:20

TỪ KHÓA LIÊN QUAN