|
|||
|
|
CONTENTSWhat Is a Form?All modern programs will toss a dialog box in your face if they need to extract some information from you. For example, selecting a program's "Print" command will most likely result in some kind of "Print" dialog box showing up. The purpose of this dialog box will be to pester you for information such as the number of copies you want, the pages you want to print, and so on. A form is simply the Web-page equivalent of a dialog box. It's a page populated with text boxes, drop-down lists, and command buttons to get information from the reader. Of course, guest books are only the beginning of what you can do with forms. If you put out a newsletter or magazine, you can use forms to gather information from subscribers; if your Web site includes pages with restricted access, you can use a form to get a person's user name and password for verification; if you have information in a database, you can use a form to have people specify what type of information they want to access. Creating FormsYou create forms using special HTML tags, and it's pretty easy to set up a form. (The problem, however, is getting hold of the information that the reader types into the form. Unfortunately, this requires some programming, so it's well beyond the scope of a humble guide such as this. So what's a poor, programming-challenged Web wizard to do? Check out the section titled "Can You CGI?" later on in this chapter.) To get started, enter the <FORM> and </FORM> tags. These can be inserted anywhere inside the body of the page. You place all the other form-related tags (which we'll show you in the rest of this chapter) between <FORM> and </FORM>. The <FORM> tag always includes a couple of extra goodies that tell the Web server how to process the form. Here's the general format: <FORM ACTION="url" METHOD=METHOD> Here, the ACTION attribute tells the browser where to send the form's data. This will almost always be a program (or script, as they're often called) that processes the data and then performs some kind of action (hence the name). The url part is the URL of the Web page that contains the program. The METHOD attribute tells the browser how to send the form's data to the URL specified with ACTION. You have two choices here for METHOD: POST and GET. Although both work in most cases, GET tends to cause errors in large forms. Therefore, you should always use the POST method. Making It Go: The Submit ButtonMost dialog boxes, as you probably know from experience, have an OK button. Forms also have command buttons, and they come in two flavors: "submit" buttons and "reset" buttons. A submit button is the form equivalent of an OK dialog box button. When the reader clicks on the submit button, the form data is shipped out to the program specified by the <FORM> tag's ACTION attribute. Here's the simplest format for the submit button: <INPUT TYPE=SUBMIT> As you'll see, most form elements use some variation on the <INPUT> tag and, as we said before, you place all these tags between <FORM> and </FORM>. In this case, the TYPE=SUBMIT attribute tells the browser to display a command button labeled Submit Query (or, on some browsers, Submit or Send). Note that each form can have just one submit button. If the standard Submit Query label is a bit too stuffy for your needs, you can make up your own label, as follows: <INPUT TYPE=SUBMIT VALUE="Label"> Here, Label is the label that will appear on the button. The following figure shows how it looks in a browser. <HTML> Starting Over: The Reset ButtonIf you plan on creating fairly large forms, you can do your readers a big favor by including a reset button somewhere on the form. A reset button clears all the data from the form's fields and re-enters any default values that you specified in the fields. Here's the tag you use to include a reset button: <INPUT TYPE=RESET> This creates a command button labeled "Reset." Yes, you can create a custom label by tossing the VALUE attribute into the <INPUT> tag, as in the following example: <INPUT TYPE=RESET VALUE="Start From Scratch"> Using Text Boxes for Single-Line TextFor simple text entries, such as a person's name or address, use text boxes. These are just rectangles within which the reader can type whatever he or she likes. Here's the basic format for a text box: <INPUT TYPE=TEXT NAME="Field Name"> In this case, Field Name is a name you assign to the field that's unique among the other fields in the form. For example, to create a text box the reader can use to enter their first name (let's call it "First"), you'd enter the following: <INPUT TYPE=TEXT NAME="First"> For clarity, you'll also want to precede each text box with a label that tells the reader what kind of information to type in. For example, the following line precedes a text box with "First Name:" so the reader knows to type in their first name: First Name: <INPUT TYPE=TEXT NAME="First"> Here's some HTML code that utilizes a few text boxes to gather some information from the reader: <HTML> You'd normally replace this ACTION attribute with one that points to a program that will do something to the data. Text boxes also come with the following bells and whistles: Setting the default value If you'd like to put some pre-fab text into the field, include the VALUE attribute in the <INPUT> tag. For example, suppose you want to know the URL of the reader's home page. To include http:// in the field (since most URLs begin with this), you'd use the following tag: <INPUT TYPE=TEXT NAME="URL" VALUE="http://"> Setting the size of the box To determine the length of the text box, use the SIZE attribute. (Note that this attribute affects only the size of the box, not the length of the entry; for the latter, see the MAXLENGTH attribute, below.) For example, the following tag displays a text box that's 40 characters long:<INPUT TYPE=TEXT NAME="Address" SIZE=40> Limiting the length of the text In a standard text box, the reader can type away until their fingers are numb. If you'd prefer to restrict the length of the entry, use the MAXLENGTH attribute. For example, the following text box is used to enter a person's age and restricts the length of the entry to 3 characters:<INPUT TYPE=TEXT NAME="Age" MAXLENGTH=3> Using Text Areas for Multi-Line TextIf you want to give your readers lots of room to type their comments, or if you need multi-line entries (such as an address), you're better off using a text area than a text box. A text area is also a rectangle that accepts text input, but text areas can display two or more lines at once. Here's how they work: <TEXTAREA NAME="Field Name" ROWS=TotalRows
COLS=TotalCols WRAP> Here, Field Name is a unique name for the field, TotalRows specifies the total number of lines displayed, and TotalCols specifies the total number of columns displayed. The WRAP attribute tells the browser to wrap the text onto the next line whenever the user's typing hits the right edge of the text area. Note, too, that the <TEXTAREA> tag requires the </TEXTAREA> end tag. (If you want to include default values in the text area, just enter them-on separate lines, if necessary-between <TEXTAREA> and </TEXTAREA>.) The following HTML tags show a text area in action:. <HTML> Toggling an Option On and Off with Check BoxesIf you want to elicit yes/no or true/false information from your readers, check boxes are a lot easier than having the user type in the required data. Here's the general format for an HTML check box: <INPUT TYPE=CHECKBOX NAME="Field Name"> As usual, Field Name is a unique name for the field. You can also add the attribute CHECKED to the <INPUT> tag, which tells the browser to display the check box "pre-checked." Here's an example: <INPUT TYPE=CHECKBOX NAME="Species" CHECKED>Human Notice in the above example that we placed some text beside the <INPUT> tag. This text is used as a label that tells the reader what the check box represents. Here's a longer example that uses a whole mess of check boxes. The following figure shows how it looks: <HTML> Multiple Choice Options: Radio ButtonsInstead of yes/no choices, you might want your readers to have a choice of three or four options. In this case, radio buttons are your best bet. With radio buttons, the user gets two or more options, but they can pick only one. Here's the general format: <INPUT TYPE=RADIO NAME="Field Name" VALUE="Value"> Field Name is the usual field name, except in this case you supply the same name to all the radio buttons. That way, the browser knows which buttons are grouped together. Value is a unique text string that specifies the value of the option when it's selected. In addition, you can also add CHECKED to one of the buttons to have the browser activate the option by default. The following HTML document puts a few radio buttons through their paces, as shown in the following figure. <HTML> Selecting From ListsRadio buttons are a great way to give your readers multiple choices, but they get unwieldy if you have more than about five or six options. For longer sets of options, you're better off using lists, or selection lists as they're called in the HTML world. Selection lists are a wee bit more complex than the other form tags we've looked at, but not by much. Here's the general format: <SELECT NAME="Field Name" SIZE=Items> As we're sure you've guessed by now, Field Name is the unique name for the list. For the SIZE attribute, Items is the number of items you want the browser to display. If you omit SIZE, the list becomes a drop-down list. If SIZE is 2 or more, the list becomes a rectangle with scroll bars for navigating the choices. Also, you can insert the MULTIPLE attribute into the <SELECT> tag. This tells the browser to allow the user to select multiple items from the list. Between the <SELECT> and </SELECT> tags are the <OPTION></OPTION> tags; these define the list items. If you add the SELECTED attribute to one of the items, the browser selects that item by default. To get some examples on the table: <HTML> What is CGI?All this form folderol is fine, but what good is a form if it doesn't really do much of anything? That is, why bother building a fancy form if you have no way to get the data? Unfortunately, as we mentioned earlier, grabbing form data and manipulating it is a programmer's job. Specifically, you have to use something called the Common Gateway Interface, or CGI for short. CGI is a method of transferring form data in a manner that makes it relatively easy to incorporate into a program and then massage it all you need. Easy, that is, if you have the requisite nerdskills. Well, we may not have room to teach you how to program forms, and you may not have the inclination in any case, but that doesn't mean you're totally stuck. The next few sections give you some ideas for getting your forms to do something useful. Ask Your ProviderMany people want to add simple guest books and feedback mechanisms to their sites, but they don't want to have to bother with the programming aspect. So, in response to their customers' needs, most Web hosting providers make some simple CGI scripts (programs) available to their customers. For example, one common type of script grabs form data, extracts the field names and values, and sends them to an e-mail address you specify. Check with the provider's administrator or webmaster to see if they have any CGI scripts that you can use. And if you haven't settled on a provider yet, you should ask in advance if they have CGI programs available. The Hire-a-Nerd RouteA more expensive alternative is to hire the services of a CGI wizard to create a custom program for you. Most Web hosting providers will be only too happy to put together a nice little program tailored to your needs. There's also no shortage of hired guns on the Web who'll create programs to your specifications. As a starting point, check out some of the resources mentioned in the next section. Check Out the Web's CGI ResourcesIf your service provider or Web hosting provider doesn't have ready-to-run CGI programs that you can use, there's no shortage of sites on the Net that are only too happy to either teach you CGI or supply you with programs. To get a list of some of these sites use: Yahoo's CGI Index A long list of CGI-related resources. Many of the links have either CGI how-to info or actual programs you can use. Here's where to go: http://www.yahoo.com/Computers_and_Internet/Internet/
World_Wide_Web/CGI_Common_Gateway_Interface The Least You Need to KnowThis chapter introduced you to the fabulous world of forms:
|
Make KacMac your home page Add URL - Information Center - Contact us - Terms of Service - Privacy Policy - Advertising |
Copyright © 2004 KacMac. All rights reserved. |