The next set of HTML tags are designed to allow you to enhance the interactivity of your Web pages by increasing your ability to request information from users. Using the forms tags, you can ask users to enter text information, choose from menus, mark checkboxes, make choices from radio buttons, and then send that information to the Web server for processing.
Although the forms tags are a part of the HTML 2.0 standard, it's
important to recognize that not all browsers are capable of viewing
them-especially older browsers and text-based browsers. Users
need to have forms-aware browsers, like the current versions of
NCSA Mosaic, Netscape Navigator, and Microsoft Internet Explorer,
among others. Generally, other browsers will simply ignore the
forms commands if they can't deal with them.
It's a good idea to let your users know that they're about to jump to a form-based page whenever possible. Forms pages are a waste of time for users of older browsers that don't support them.
The idea behind a Web form is simple-it allows you to accept information or answers from your users with varying levels of guidance. Users can be asked to type answers, choose their answers from a list of possibilities you create, or even be limited to choosing one answer from a number of options that you specify.
That data is then passed on to the Web server, which hands it
to a script, or small program, designed to act on the data and
(in most cases) create an HTML page in response. In order to deal
with forms data then, you need to understand a little something
about scripting, or programming, for a Web server-or know someone
who does. While learning to program is beyond the scope of this
book, we'll look at how these scripts work in Chapter 14,
"Form Design and Data Gathering with CGI Scripts."
Most Web server scripts are written in Perl, C, or UNIX shell scripts. If your Web server is DOS, Windows, or Mac based, however, you may have other options. Some DOS Web servers allow you to script in the DOS batch language, while some Windows servers can accept Visual Basic scripts (not to be confused with Microsoft's new Visual Basic Script language). Mac Web servers generally allow for AppleScript or Frontier scripting.
In an HTML document, forms are set between the <FORM> container tags. The form container works as follows:
<FORM METHOD="how_to_send" ACTION="URL of script">
Notice that the <FORM> tag takes two attributes: METHOD and ACTION. The METHOD attribute accepts either POST or GET as its value. POST is by far the more popular, as it allows for a greater amount of data to be sent. GET is a little easier for Web programmers to deal with, and is best used with single responses, like a single textbox.
The second attribute is ACTION, which simply accepts the URL for the script that will process the data from your form. Most often the script is stored in a directory called bin/ or cgi-bin/ located on your Web server.
An example of the <FORM> tag then, would be the following:
<FORM METHOD="SEND" ACTION="http://www.fakecorp.com/cgi-bin/register_script">
As with any HTML container tag, this implementation of the <FORM>
tag has actually created a complete form (just like <P>
and </P> is a complete
paragraph). Unfortunately, our complete form doesn't do
anything yet, so that's somewhat academic.
You can't nest forms within one another. You need to add the end tag </FORM> for the first form before creating another one in the same document. Generally, browsers will ignore any new occurrences of the <FORM> tag, since the purpose of the tag is to tell the browser how to submit data to the server, and different parts of the form can't be submitted in different ways.
Let's take a quick look at a form that's been created by someone else-one that most seasoned Web browsers have encountered at one time or another. Load up your Web browser and point it to http://webcrawler.com/.
This is the WebCrawler page, a Web search engine offered by America Online. Your next step is to view the source of this document. Select the View Document Source command in your Web browser's Edit menu. What you see will look something like figure 13.1.
Figure 13.1 : Example of an HTML form available on theWeb.
Nearly all graphical browsers have a View Source command. Look in the Edit menu for this command or a command with a similar name. The HTML source of the current Web document will then be displayed or saved as a text file.
Notice a couple of things here. The <FORM>
tag at WebCrawler is using the ACTION
and METHOD attributes that
were discussed. ACTION is
accessing a script called WebQuery
found in the cgi-bin/ directory
of the Web server. The METHOD
used is SEND.
Although you shouldn't copy others' work, don't forget that you can always use View Source commands to learn how something was done on the Web.
One of the more common uses for forms is to accept multiple lines of text from a user, perhaps for feedback, bug reports, or other uses. To do this, use the <TEXTAREA> tag within your form. You can set this tag to control the number of rows and columns it displays, although it will generally accept as many characters as the user desires to enter. It takes the following form:
<TEXTAREA NAME="variable_name" ROWS="number" COLS="number">
It may surprise you to find that <TEXTAREA> is a container tag, since it just puts a box for typing on the page. What's contained in the tag is the default text-so you can guide your users by letting them know what you'd like entered there. For instance:
<TEXTAREA NAME="comments" ROWS="4" COLS="40">
Enter comments about this Web site.
Good or Bad.
The default text appears in the textbox just as typed. Notice in figure 13.2 that text inside the <TEXTAREA> tag works like <PRE> formatted text. Any returns or spaces you add to the text are displayed in the browser window. In fact, notice that by hitting Return after the opening <TEXTAREA> tag, I'm inserting a blank line at the top of the textarea (in many browsers).
Figure 13.2 : The <TEXTAREA> tag in action.
The NAME attribute is a variable name for this string of text. It gets passed on to your processing script on the Web server. ROWS and COLS can accept different numbers to change the size of the textarea box, but you should take care that the majority of browsers can see the entire box on-screen. It's best to limit COLS to 80, and ROWS to something like 24 (the typical size for a text-based computer screen). But it's up to you.
<TEXTAREA> will also accept one other attribute: WRAP. WRAP can be set to OFF (which is the default if WRAP is not included), VIRTUAL, or PHYSICAL. Setting wrap to PHYSICAL forces the browser to include actual line breaks in the text when sending it to the Web server. VIRTUAL makes the textbox seem to offer line wrap, but sends a continuous stream of words to the Web server (unless the user has entered returns on his or her own).
I mentioned before that <TEXTAREA> is commonly used to gather feedback from your Web users. To create a small form to do just that, save your default template as a new HTML document and enter the following:
<P>Please take a moment to tell us what you thought of the Web site.<BR>
Your Feedback is appreciated!</P>
<FORM METHOD="POST" ACTION="cgi-bin/feedback">
Enter your comments below:<BR>
<TEXTAREA NAME="comments" ROWS="10" COLS="70" WRAP="VIRTUAL">
You can see how this looks in figure 13.3. Notice in the example that some descriptive text is enclosed inside the <FORM> tag, but outside of the <TEXTAREA> tag. This is completely legal-it just lets you explain what the purpose of the textarea is.
Figure 13.3 : Sample textarea HTML form.
You may have realized that there's something lacking in this sample form. There's no way to submit the user's entry! You'll get to that in the next section, when I discuss this next tag for form entry.
Our next tag for HTML forms give you the opportunity to be a bit more picky about the type of input you're going to accept from the user. The <INPUT> tag follows the following format:
<INPUT TYPE="type_of_box" NAME="variable" SIZE="number" MAXLENGTH="number">
Now, technically, the only required attributes are TYPE
and NAME. Some other "types"
of the input tag will also accept the attribute VALUE.
But first, let's look at the different types of <INPUT>.
By the way, notice that <INPUT> is an empty tag. There's no </INPUT> element.
The first possible value for the TYPE attribute is TEXT, which creates a single-line textbox of a length you choose. Notice that the length of the box and the maximum length entered by the user can be set separately. It's possible to have a box longer (or, more often, shorter) than the maximum number of characters you allow to be entered. Here's an example of a textbox:
Last name: <INPUT TYPE="TEXT" NAME="last_name" SIZE="40" MAXLENGTH="40">
When appropriately entered between <FORM> tags, this <INPUT> yields a box similar to figure 13.4. If desired, the attribute VALUE can be used to give the textbox a default entry, as in the following example:
Figure 13.4 : Using the TEXT option with the TYPE attribute.
Type of Computer: <INPUT TYPE="TEXT" NAME="computer" SIZE="50" MAXLENGTH="50" VALUE="Pentium">
The PASSWORD option is nearly identical to the TEXT option except that it responds to typed letters with bullet points or a similar scheme (chosen by the browser) to keep the words from being read. A sample password box could be the following:
Enter Password: <INPUT TYPE="PASSWORD" NAME="password" SIZE="25" MAXLENGTH="25">
When characters are typed into this textbox, they are shown on the screen as in figure 13.5.
Figure 13.5 : PASSWORD hides text from people looking over your user's shoulder.
Recognize that the text is still stored as the text typed by the user-not as bullet points or similar characters.
This value for TYPE allows you to create a checkbox-style interface for your form. This is best used when there are two possible values for a given choice-and no others. You can also determine whether or not a checkbox will already be checked (so that it must be unchecked by the user, if desired), by using the attribute CHECKED. Here's an example of adding checkboxes to a form:
Type of computer(s) you own:<BR>
<INPUT TYPE="CHECKBOX" NAME="Pentium" CHECKED> Pentium
<INPUT TYPE="CHECKBOX" NAME="486"> 486-Series PC
<INPUT TYPE="CHECKBOX" NAME="Macintosh"> Macintosh
In this example, it's possible to check as many of the options as are presented. CHECKBOX evaluates each item separately from any others. Figure 13.6 illustrates how CHECKBOX is displayed in a browser.
Figure 13.6 : Notice that Pentium is prechecked.
Like CHECKBOX, RADIO is designed to offer your user a choice from pre-determined options. Unlike CHECKBOX, however, RADIO is also designed to accept only one response from among its options. RADIO uses the same attributes and basic format as CHECKBOX.
RADIO requires that you use the VALUE attribute, and that the NAME attribute be the same for all of <INPUT> tags that are intended for the same group. VALUE, on the other hand, should be different for each choice. For instance, look at the following example:
Choose the computer type you use most often:<BR>
<INPUT TYPE="RADIO" NAME="Computer" VALUE="P" CHECKED> Pentium
<INPUT TYPE="RADIO" NAME="Computer" VALUE="4"> 486-Series PC
<INPUT TYPE="RADIO" NAME="Computer" VALUE="M"> Macintosh
<INPUT TYPE="RADIO" NAME="Computer" VALUE="O"> Other
With RADIO, it's important
to assign a default value, since it's possible that the user will
simply skip the entry altogether. While the user can't check more
than one, he or she can check none.
So choose the most common value and set it as CHECKED,
just so that the form-processing script doesn't have trouble.
Of course, if you want to give your user the option of choosing none, then you can leave off the CHECKED attribute. It's more complete and obvious for the user, however, to include another radio button with a VALUE of none, and make it the CHECKED choice.
This <INPUT> type technically
isn't "input" at all. Rather, it's designed to pass
some sort of value along to the Web server and script. It's generally
used to send a keyword, validation number, or some other kind
of string to the script so that the script knows it's being accessed
by a valid (or just a particular) Web page. The <INPUT
TYPE="Hidden"> tag takes the attributes
NAME and VALUE.
This isn't really terribly covert, since an intrepid user could simply choose View Source to see the value of the hidden field. It's more useful from a programmer's standpoint. For instance, on a large Web site, the hidden value might tell a multi-purpose script which particular form (among many) is sending the data, so the script knows how to process the data.
The <INPUT> tag has built into it the ability to clear an HTML form. RESET simply creates a push button (named with the VALUE string) that resets all of the elements in that particular FORM to their default values (erasing anything that the user has entered). An example would be the following:
With a VALUE statement, you could enter the following:
<INPUT TYPE="RESET" VALUE="Reset the Form">
The results are shown in figure 13.7.
Figure 13.7 : Default and VALUE-attributed Reset buttons.
The <INPUT> tag also has a type that automatically submits the data that's been entered into the HTML form. The SUBMIT type accepts only the attribute VALUE, which can be used to rename the button. Otherwise, the only purpose of the Submit button is to send off all the other form information that's been entered by your user. See the following two examples (see fig. 13.8):
Figure 13.8 : Creating a Submit button.
<INPUT TYPE="SUBMIT" VALUE="SEND IT IN!">
You can use just about anything you want for the VALUE, although it's best to remember that really small words, like OK, don't look great as buttons. To make a button larger, enter the VALUE with spaces on either end, like in the following:
<INPUT TYPE="SUBMIT" VALUE=" GO ">
Along with all the other <INPUT> types, now you've finally got a way to submit data. So, let's create a more involved form that includes some of these examples-a subscription form.
Save your HTML template to create a new document. Then, enter something similar to Listing 13.1.
Listing 13.1 scrp_frm.html Creating a Complete Form
<H2>Subscribe to CorpWorld</H2>
<P>Interested in receiving daily email updates of all the latest exploits of BigCorp? Well, now you can. And, best of all, it's free! Just fill out this form and submit it by clicking the "Send it In" button. We'll put you on our mailing list, and you'll receive your first email in 3-5 days.</P>
<FORM METHOD="Send" ACTION="http://www.fakecorp.com/cgi-bin/subscribe">
Please complete all of the following:<BR>
First Name: <INPUT TYPE="Text" Name="first" SIZE="25" MAXLENGTH="24"><BR>
Last Name: <INPUT TYPE="Text" Name="last" SIZE="35" MAXLENGTH="34"><BR>
Business: <INPUT TYPE="Text" Name="business" SIZE="50" MAXLENGTH="49"><BR>
We must have a correct email address to send you the newsletter:<BR>
Email: <INPUT TYPE="Text" Name="email" SIZE="50" MAXLENGTH="49"><BR>
How did you hear about BigCorp's email letter?<BR>
<INPUT TYPE="RADIO" NAME="hear" VALUE="web" CHECKED>Here on the Web
<INPUT TYPE="RADIO" NAME="hear" VALUE="mag">In a magazine
<INPUT TYPE="RADIO" NAME="hear" VALUE="paper">Newspaper story
<INPUT TYPE="RADIO" NAME="hear" VALUE="other">Other
<BR> Would you care to be on our regular mailing list?<BR>
<INPUT TYPE="CHECKBOX" NAME="snailmail" CHECKED> Yes, I love junk mail<BR>
<INPUT TYPE="SUBMIT" VALUE="Send it in!">
Notice that, for text type <INPUT> boxes, the MAXLENGTH is one less than the size of the box. This tends to look a little better, but choosing the size is up to you. Figure 13.9 shows how it looks on a Web page. (You'll get to straightening everything out and making it look great in Chapter 14.)
Figure 13.9 : The complete form in MS Internet Explorer.
The last types of input that you can offer to users of your Web page revolve around the <SELECT> tag, which can be used to create different types of pop-up and scrolling menus. This is another element designed specifically for allowing users to make a choice-they can't enter their own text. The <SELECT> tag requires a NAME attribute and allows you to decide how many options to display at once with the SIZE attribute.
Also notice that, like <TEXTAREA>, <SELECT> is a container tag. Options are placeed between the two <SELECT> tags, each with a particular VALUE that gets associated with <SELECT>'s NAME attribute when chosen. The following is the basic format:
<OPTION SELECTED VALUE="value"> Menu text
<OPTION VALUE="value"> Menu text
The attribute SELECTED is simply designed to show which value will be the default in the menu listing. value can be anything you want to pass on to the Web server and associated script for processing. An example might be:
Choose your favorite food:
<OPTION SELECTED VALUE="ital"> Italian
<OPTION VALUE="texm"> TexMex
<OPTION VALUE="stek"> SteakHouse
<OPTION VALUE="chin"> Chinese
You can also use the SIZE attribute to decide to display the menu in its entirety, by simply changing the first line of the example to the following:
<SELECT NAME="food" SIZE="4">
Both examples are shown in figure 13.10.
Figure 13.10 : Two <SELECT> menus-a pop-up and a fixed.
In the first example, selecting the menu item with the mouse causes the menu to pop-up on the page. The user can then select from the choices. In the second example, it's necessary to click the desired item.
One more attribute for the <SELECT> tag allows the user to select more than one option from the menu. Using the MULTIPLE attribute forces the menu to display in its entirety, regardless of the SIZE attribute. An example might be the following: (the result appears in figure 13.11):
Figure 13.11 : A <SELECT> menu can allow multiple choices.
What type of cars does your family own (select as many as apply)?
<SELECT NAME="cars" MULTIPLE>
<OPTION VALUE="sedan"> Sedan
<OPTION VALUE="coupe"> Coupe
<OPTION VALUE="mivan"> Minivan
<OPTION VALUE="covan"> Conversion Van
<OPTION VALUE="statn"> Stationwagon
<OPTION VALUE="sport"> SUV (4x4)
<OPTION VALUE="truck"> Other Truck
With all of these possibilities for the form, you can manage some fairly complete data entry interfaces for users. Consider this one: an online order form. Used in conjunction with a secure Web site, this form could be used to process purchase orders over the Internet.
Save your template as a new HTML file and enter Listing 13.2's example text between the <BODY> tags.
Listing 13.2 ordr_frm.html Creating an Order Form
<H3>Online Order Form</H3>
<P> Please enter your name, billing address and shipping address. Please
don't forget the order number from our online catalog listings. Thanks for shopping BigCorp!</P>
<FORM METHOD="SEND" ACTION="http://www.fakecorp.com/cgi-bin/order">
Please enter a full name and address for BILLING purposes:<BR>
First Name: <INPUT TYPE="TEXT" NAME="first" SIZE="25" MAXLENGTH="24"><BR>
Last Name: <INPUT TYPE="TEXT" NAME="last" SIZE="35" MAXLENGTH="34"><BR>
Address: <INPUT TYPE="TEXT" NAME="address" SIZE="60" MAXLENGTH="59"><BR>
City: <INPUT TYPE="TEXT" NAME="city" SIZE="25" MAXLENGTH="24">
State: <INPUT TYPE="TEXT" NAME="state" SIZE="3" MAXLENGTH="2"> ZIP:
<INPUT TYPE="TEXT" NAME="zip" SIZE="6" MAXLENGTH="5"><BR>
<INPUT TYPE="CHECKBOX" NAME="same_add"> Check if Shipping Address is
different from Mailing Address
<TEXTAREA NAME="ship_add" ROWS="3" COLS="60" WRAP="PHYSICAL">Enter shipping address here if different from above.
Please enter the code for the product you wish to purchase: <INPUT TYPE=
"TEXT" NAME="prod_num" SIZE="7" MAXLENGTH="6">
How would you like to pay for this?<BR>
<OPTION SELECTED VALUE="mast"> MasterCard
<OPTION VALUE="visa"> Visa
<OPTION VALUE="amex"> American Express
<OPTION VALUE="disc"> Discover
Please enter the card number: <INPUT TYPE="TEXT" NAME="cred_num" SIZE="17"
Expiration date (01/99): <INPUT TYPE="TEXT" NAME="exp_date" SIZE="6"
Please take care that everything is filled out correctly, then click "Submit Order." If you'd like, you can select the "Reset" button to start again. Clicking the "Submit Order" button will send your order to BigCorp and your credit card will be charged.<BR>
<INPUT TYPE="submit" VALUE="Submit Order">
Here you've taken advantage of most of the options available to you for forms (see fig. 13.12). Notice that, if the checkbox for Check if Shipping Address is different from Mailing Address is left unchecked, you can assume (in the processing script) that the textarea can be ignored. Also notice how using the MAXLENGTH attribute for State: and ZIP: allows you some very basic error checking in these fields. At least, you know that users are entering the correct number of characters.
Figure 13.12 : The completed Web order form.
Once the user clicks the Submit Order button, the script on your Web server takes over. The script should be designed to accept the data, add it to your internal order-processing database (if appropriate), and respond to the submission with an HTML page confirming the order and offering any additional help or instructions. Then, hopefully, the product will ship on time!
HTML forms are a powerful way to add interactivity to your Web site. They can be used to elicit information, responses, memberships, or even product orders from your users. They can also be used as an interface for data retrieval.
The basic elements of a form are the <FORM> tag itself, along with a number of different types of form elements, including <INPUT>, <SELECT>, and <TEXTAREA>. Each of these have their own attributes, values, and special cases.
The <TEXTAREA> creates a relatively free-form textarea where comments, messages, and other feedback can be typed by the user. <INPUT> allows for a number of different types of interaction with the user, including single-line textboxes, radio button interfaces, checkboxes, and special buttons for resetting forms or sending in the form data.
<SELECT> types allow you to control how users respond by offering them access to menu listings. Menus can be either pop-up or scrolling, giving the user the ability to make a single choice or multiple choices from each menu.