- Introduction
- Getting Started
- Creating Forms with HTML Tags
- Controls: Names, Values, and Labels
- Design Heuristics for Great-Looking Forms
- Glossary
Controls: Names, Values, and Labels
Controls get information from users and pass this information to scripts, such as the Active Server Pages scripts we'll discuss in our next tutorial. The scripts do the actual work of putting the information into the database (refer to the left half of Figure 1). A form is useless without a corresponding script.
There are five basic kinds of controls:
Text boxes, which includes multiple-line text boxes
Check boxes
Radio buttons
Push buttons
Drop-down lists
NOTE
When a text box includes an arrow button to open a drop-down list, that control is commonly referred to as a combo box.
In this tutorial, we'll show you how to use text boxes, check boxes, radio buttons, and push buttons to make a form for your Guest book. The form will get all the user information that we need for your guestbook database table. When we're done, we'll have a form that looks like Figure 14.
Figure 14 Browser: The next few sections show you how to create a form for users to enter information into your Guest book.
For controls in a form to properly communicate user-information to scripts, you must give every control a unique name. A valid control name starts with a letter and is followed by any combination of letters, numbers, and underscore characters (_). For example, if you want to include a text box in your form to get a user's email address, a good name for that text box might be email or e_mail. However, e-mail or e mail would not be good names, since they include characters that not a letter, number, or underscorea hyphen and a space, respectively. Control names are not case sensitive, so EMAIL, email, and EmAiL are actually all the same name as far as your form and script are concerned.
In addition to names, controls have values. The value of a control is whatever the user enters into that control. When a control first appears in a web page, it's usually empty unless you set a default value. For example, if you have a text box to get the user's address instead of having it come up blank, you might set the default value to be Please fill in your address. You can also set default values for check boxes and radio buttons. Note that in our Guest Book form, we set the default value for the check box labeled Hide Email to be checked when the form first appears; for gender, the radio button for the female option is checked as the default.
Finally, for the user's sake, you should label each control. A control's label is not part of the control per se. You simply use HTML to add information to the left or right of the control (and sometimes to the top and bottom of each control) so the user knows what the control is there for.
The following sections show you how to create controls and give them names, default values, and labels.
Single-Line Text Boxes
The most common control you encounter on a web page form is the one-line text box, hereafter simply called a text box. You use text boxes whenever you need to get textual information (such as a name), numerical information (such as the user's age), or combination text-and-numerical information (such as an address).
To create a text box, you use the <INPUT> tag and set its TYPE to TEXT. As mentioned earlier, each control must have a name, which you set using the NAME parameter. The minimum code for a text box is as follows, where unique_name is a unique name that you assign to the text box:
<INPUT TYPE=TEXT NAME=unique_name>
The command above creates a one-line text box that can hold approximately 20 characters.
NOTE
Did you notice that <INPUT> is one of those tags that doesn't need a matching closing tag?
Example: Creating a Text Box
Our Guest Book form has a text box to get the user's name. To create this text box, we use the following command, in which the unique name of our text box is simply name:
<INPUT TYPE=TEXT NAME=name>
-
Type the command above into Notepad, between the <FORM> and </FORM> tags (see Figure 15).
Save the file.
-
Refresh the guestbook.html file in your browser (see Figure 16).
Figure 15 Notepad: Adding a text box to get the user's name.
Figure 16 Browser: A one-line text box.
Not very exciting, is it? It needs a label that tells the user what the text box is for. To add a label, simply type some text to the left of the input control in the guestbook.html file. Since this text box gets the user's name, we'll add the label Name: to the left of the text box:
-
In guestbook.html, type Name: and a space before the <INPUT> tag (see Figure 17). The space isn't required, but it helps to make the code more readable.
Save the file.
-
View the revised HTML file in your browser (see Figure 18).
Figure 17 Browser: Adding a label for the text box.
Figure 18 Browser: With the label in place, users know instantly what they're supposed to do.
As you can see, creating text boxes is not very difficult at all. Text boxes have a number of useful parameters, of which SIZE is the most commonly used. The next section provides the details.
The SIZE Parameter
The default text box holds approximately 20 characters. You can change this number via the SIZE parameter. Here's the syntax:
<INPUT TYPE=TEXT NAME=unique_name SIZE=number_of_characters>
So, to create a text box to hold 40 characters, you would set SIZE=40. Let's do that in guestbook.html:
-
Add SIZE=40 inside the <INPUT> tag (see Figure 19).
Save the file.
-
View the revised guestbook.html in your browser (see Figure 20).
Figure 19 Notepad: Specifying the SIZE parameter for the text box.
Figure 20 Browser: The text box with the SIZE parameter set. Compare the size of the text box with that in Figure 18.
You can actually put the SIZE parameter anywhere in the tag after the word INPUT, but by convention <INPUT> tags specify the TYPE parameter followed by NAME, followed by any other parameter such as SIZE.
Now it's your turn. Note that our Guest Book form has two other one-line text boxes: email and age (refer to Figure 14). In the following exercises, you'll add a text box for email and one for age. But first, you need to learn about the break command (<BR>).
The <BR> (Break) Tag
Before you add a text box for the email address, add a break tag (<BR>) after the Name text box so that your E-mail text box will start on a separate line (see Figure 21). The <BR> tag has no matching closing tag. Its purpose is simply to tell the browser that whatever comes after <BR> starts on the next line.
Figure 21 Notepad: Adding a <BR> tag to start the next control on a new line.
Text Box Exercise 1: Adding a Text Box
Create a one-line text box named email (with the label E-mail:) that can hold 40 characters.
Add a break tag after this new control.
-
If you've implemented the text box correctly, your file should look like the one in Figure 22. Save the file.
-
View the new version in your browser (see Figure 23).
Figure 22 Notepad: The code for the E-mail text box is now in place.
Figure 23 Browser: Guest Book form with Name and E-mail text boxes.
If you're thinking, "This isn't hard at all," you're right. It isn't. Let's do one more exercise, but first let's learn a new parameter, VALUE, which you can use to set a default value when your text box is initially displayed.
The VALUE Parameter
You use the VALUE parameter to set the default value for a text box when the browser first displays the form containing that text box. For example, a text box specifying a country might have United States filled in as the default. The general syntax for creating a text box with a default value is as follows:
<INPUT TYPE=TEXT NAME=unique_name SIZE=number VALUE="default">
Notice that the value appears in double quotes.
Let's create an age box with a default value specified.
Text Box Exercise 2: Specifying a Default Value
Create a one-line text box named age (with the label Age:) that can hold three charactersin case the user happens to be 102, for example. Set the default age to 18.
Add a break tag after this new control.
-
If you've implemented the text box correctly, your file should look like the one in Figure 24. Save the file.
-
View the new version in your browser (see Figure 25).
Figure 24 Notepad: This text box will ask for the user's age.
Figure 25 Browser: Guest Book form with Name, E-mail, and Age text boxes.
Next we'll look at how to create check box and radio button controls.
Check Boxes
You use check boxes whenever you have a category in your form with several choices, and the user can select none, any combination, or even all of the choices. For example, if you want to know what kinds of music your users like, you might create a form with check boxes for rock-and-roll, funk, blues, jazz, country, and classical, to name a few. The user can select one or more of these choices, or none at all.
To create a check box, you use the <INPUT> tag and set its TYPE to CHECKBOX. Each check box on your form must have a unique name, of course, which you set using the NAME parameter. Is this sounding familiar? Here's how check boxes are different from text boxes: You must set a value for the check box, using the VALUE parameter. If the user selects a check box, the value in the VALUE parameter is sent to the form's script. Remember, the script is specified by the ACTION parameter in the <FORM> tag; for example, in the Guest Book form the script is insert.asp.
The minimum command to create a check box is as follows:
<INPUT TYPE=CHECKBOX NAME=unique_name VALUE="value">
where unique_name is a unique name that you assign to the control and value is what you want the check box to return to the script if the check box is selected by the user. This value is arbitrary, but most web form designers return true, yes, or the number 1.
Example: Creating a Check Box
In our Guest Book form, we'll give the user the option of hiding his or her email address from other users who view the guest book. A check box is the perfect control for this application. If the check box is "checked" (selected), the user wants to hide his or her email address, and we'll set the value of the check box to yes. To create this check box, we'll use the following command, in which the unique name of our check box is simply hideEmail:
<INPUT TYPE=CHECKBOX NAME=hideEmail VALUE="yes">
Notice that the VALUE parameter is set to return yes if the check box is checked.
Type the command line above into the guestbook.html file, after the email text box code.
Add the label Hide E-mail to the right of the check box. This differs from text boxes, where you usually add the label to the left of the control.
-
Add a <BR> tag after the label so that the next control will start on a new line (see Figure 26).
-
Save the file and view the new version in your browser (see Figure 27).
Figure 26 Notepad: Adding a check box to the Guest Book form.
Figure 27 Browser: The Guest Book form with Name, E-mail, Hide E-mail, and Age controls.
Note that the default state of a check box is unchecked. If you know that most of your users will select a given choice, you may want to set up the check box so that it's checked when the form first displays. For example, to minimize email spam, most users probably don't want their email addresses displayed. Thus, the default state of our hideEmail check box should be checked.
The CHECKED Parameter
To set up a check box so that it's checked when first displayed, you use the CHECKED parameter. CHECKED is a valueless parameter. You simply add it to the <INPUT> tag for any check box control that you want checked:
-
Add the CHECKED parameter to the hideEmail check box (see Figure 28).
-
Save the file and view the revised version in your browser (see Figure 29).
Figure 28 Notepad: The CHECKED parameter is added to the hideEmail check box.
Figure 29 Browser: The Hide E-mail? check box will be checked when the form opens in the browser.
TIP
They're called check boxes, and if they're selected we say they're checked, but not all browsers actually display a check mark in a check box. Some display an X instead.
Next we look at a close relative of check boxesradio buttons.
Radio Buttons
You use radio buttons whenever a category in your form has more than one choice but the user can only select one of the choices. This is the opposite of a check box, where you can select more than one choice. Typical radio button categories include gender, marital status, and yearly income.
To create a radio button, you use the <INPUT> tag and set its TYPE to RADIO. As with the check box and text box controls, you set a radio button's name using the NAME parameter. However, the naming rules are slightly different for radio buttons. Each radio button on your form that belongs to the same category should have the same name; radio buttons in different categories should have different names. For example, suppose you have two categories on your form. The gender category has radio buttons for male and female, and the marital status category has radio buttons for single, married, and so on. All the radio buttons in the gender category will have the same name (gender) and all the radio buttons in the marital status category will have the same name (status).
Finally, as with check boxes, you set a value for the radio buttons using the VALUE parameter.
The minimum command to create a radio button looks like this:
<INPUT TYPE=RADIO NAME=category_name VALUE="value">
where category_name is a unique name that you assign to the control and value is what you want the radio button to return to the script if the radio button is selected.
Example: Creating Radio Buttons
Our guest book asks the user to specify his or her gender as male or female. To create the radio buttons, we'll use the following commands:
<INPUT TYPE=RADIO NAME=gender VALUE="male"> <INPUT TYPE=RADIO NAME=gender VALUE="female">
Notice that both radio buttons have the same NAME, gender, because they belong to the same category. However, the radio button for a male has its VALUE parameter set to "male" and the radio button for female has its VALUE parameter set to "female". To implement these radio buttons, follow these steps:
Add the above commands to the guestbook.html file, after the age text box code.
Add the label Gender: before the radio buttons.
-
Add a <BR> tag after the label and each radio button (see Figure 30).
-
Save your file and view the new version in your browser (see Figure 31).
Figure 30 Notepad: Adding radio buttons to the Guest Book form.
Figure 31 Browser: The Guest Book form with Name, E-mail, Hide E-mail, Age, and Gender controls.
Notice that, like check boxes, by default radio button are not selected. If you want to pre-select a radio button, you use the CHECKED parameter as shown in the next section.
Example: Using the CHECKED Parameter with Radio Buttons
To set up a radio button so that it's checked when first displayed, you use the CHECKED parameter just as you do with check boxeswith one exception. A given group of radio buttons can have only one button CHECKED.
In our guest book, we want the radio button for female to be the default selection. Let's set that up now:
-
Add the CHECKED parameter to the radio button labeled female (see Figure 32).
-
Save the file and view the results in the browser (see Figure 33).
Figure 32 Notepad: The CHECKED parameter, used for a radio button.
Figure 33 Browser: Female is set as the default gender for this form.
Our Guest Book form is almost done. The final control we'll cover is the multiple-line text box.
Multiple-Line Text Boxes
Sometimes you need more than a single line of information from the userfor example, if you're asking the user for comments. If you don't know how many lines of information you'll get, use a multiple-line (or multi-line) text box. Unlike the previous controls, which we created using the INPUT tag with different values for the TYPE parameter, a multi-line text box has its own tags: <TEXTAREA></TEXTAREA>. You'll see why the closing tag is needed when we get to the section on default values.
Like other tags, the <TEXTAREA> tag has a NAME parameter that you set to a unique value. The minimum command for a multi-line text box is as follows:
<TEXTAREA NAME=unique_name> </TEXTAREA>
where unique_name is some unique name you assign to the multi-line text box.
Example: Creating a Multiple-Line Text Box
We want our users to add a comment to our guest book. The user's comment could be something as short as great job or a lengthy exposition on how our web site could be changed for the better. We'll create a multi-line text box for users to add comments as follows:
<TEXTAREA NAME=comment> </TEXTAREA>
Add the above commands to guestbook.html, after the radio-buttons.
Add the label Comment: to the left of the <TEXTAREA> tag.
-
Add a <BR> command to the right of the </TEXTAREA> tag (see Figure 34).
-
Save your file and view the results in the browser (see Figure 35).
Figure 34 Notepad: Adding a multi-line text box to the Guest Book form.
Figure 35 Browser: The Guest Book form now has a multi-line text box for user comments.
Note that the comment box is fairly small: two lines, each of which can hold approximately 20 characters. What's nice is that the <TEXTAREA> will automatically wrap if the user's line exceeds 20 characters and the scrollbar will activate automatically if the number of lines is greater than two. Nevertheless, visually it might be nicer if you could specify the dimensions of the multi-line text box. In the next section we discuss the parameters to do just that.
The ROWS and COLS Parameters
If you want a multi-line text box whose dimensions are other than the default (approximately 20 characters by 2 lines), you set appropriate values for the ROWS and COLS parameters. Here's the general syntax:
<TEXTAREA NAME=unique_name ROWS=row_number COLS=column_number> </TEXTAREA>
where row_number is the number of rows in your text box and column_number is the number of columns (characters) per row. As an example, let's create a text box that has 5 rows and 40 characters per row.
Exercise: Setting the Dimensions of a Multiple-Line Text Box
By now you should be used to adding parameters in tags. Add appropriate ROWS and COLS parameters inside your <TEXTAREA> tag to create a comment box that's 5 rows high and 40 characters wide (see Figure 36). After you have saved the file and refreshed the view in your browser, your results should look like Figure 37. (Compare this comment box with the one shown earlier in Figure 35.)
Figure 36 Notepad: Using the ROWS and COLS parameters to specify the comment box size.
Figure 37 Browser: The comment box is now 5 rows long by 40 columns wide.
In addition to setting the dimensions of a multi-line text box, you can easily set a default value to appear inside.
Setting a Default Value for a Multiple-Line Text Box
You may be wondering why you need the </TEXTAREA> closing tag. Any information you add between the <TEXTAREA> and </TEXTAREA> tags becomes the default value for your multi-line text box. This is unlike a one-line text box, in which you use the VALUE parameter to set the default value. It makes sense for the default value of a <TEXTAREA> to be set this way, because the default text may itself be longer than a single line. This is the general syntax for a <TEXTAREA> with a default value:
<TEXTAREA NAME=unique_name ROWS=row_number COLS=column_number> default value goes here </TEXTAREA>
You can pretty much do these on your own now, right? Add the phrase Insert a Comment Here as the default value for the comment box in guestbook.html (see Figure 38). Figure 39 shows the result in the browser.
Figure 38 Notepad: Adding a default value for <TEXTAREA>.
Figure 39 Browser: The comment box now includes a default value.
Our form is practically done! We simply need to add a submit button to complete it.
The Submit Button
Every form needs one submit button. When the user clicks the submit button, the browser sends the user's information to the script specified in the ACTION parameter for the <FORM> tag (insert.asp in our example). No submit button, no action taken!
Creating a submit button is simple. Use the <INPUT> tag, set its TYPE parameter to SUBMIT, and set its VALUE parameter to whatever label you want on the button. (It doesn't have to say Submit, as it does on so many forms on the webthat's just laziness on the part of the programmer.)
Unlike the other controls, no NAME is required for the submit button. The general syntax for a submit button is as follows:
<INPUT TYPE=SUBMIT VALUE="button_label">
Generally, the submit button is the last control in your form. For example, if we wanted to add a submit button for our guest book, the following command would do nicely:
<INPUT TYPE=SUBMIT VALUE="SIGN MY GUESTBOOK">
Let's finish our guest book by adding the submit button. Place it right after the comment box (see Figure 40) and check your results against Figure 41.
Figure 40 Notepad: Adding a submit button.
Figure 41 Browser: Our completed Guest Book form!
That's it! Our form is done. Problem is, it's functional but not particularly appealing. It looks nothing like the "pretty" form in Figure 14. In the next section we quickly cover some design heuristics for making great-looking forms.