Forms provide a uniquely interactive element in a Web site. Through a form you not only convey content, you also collect content. This content can range from orders for products, feedback on site content, service requests, and subscription list sign-ups to surveys, forum discussions, and opinion polls.
Some form content is managed using scripts that run in the visitor’s browser. Such scripts are referred to as client-side data handling. A jump menu, for example, collects data (the page a visitor to your Web site wants to go to, for example) and acts on that input (by opening a new Web page). The client-side script does that without sending any data to a server. Other forms collect data and send it to a server, where scripts on the server manage the data. These are called server-side forms. Most form data is managed by server-side scripts. One example of a server-side script is a mailing list form. Visitors enter information (at least an email address and maybe more) into a form. That data is then stored in a database on a remote server. It can be accessed to send out mailings.
In short, this chapter explains how to design two kinds of forms:
• Forms that manage data in the browser (client-side)
• Forms that connect to scripts at a server (server-side)
In this chapter, you’ll learn how to connect a form to an existing server script (but not how to program the scripts). I’ve also thrown in some tips on where you can find already-packaged server scripts to handle things like search forms, sign-up mailing lists, and discussion forums.
One great example of a client-side form is a jump menu from which a visitor selects a page in your Web site from a pop-up menu. A jump menu works because script (in this case, JavaScript) acts on a form and effects an action (in this case, opening a new Web page) based on data the visitor entered into the form (the page the visitor chose from the jump menu). Dreamweaver creates jump menu forms and automatically generates the required JavaScript.
Jump menus are an efficient and attractive way to allow visitors to navigate your site. You can provide a long list of target links in a jump menu without using much valuable space on your Web page.
To create a jump menu, follow these steps:
To edit an existing jump menu, you need to open the behavior that Dreamweaver created to control the jump menu. View the Behaviors panel (choose Window > Behaviors). Click the jump menu to select it. As you do, you will see Jump Menu listed in the second column of the Behaviors panel. Double-click it to reopen the Jump Menu dialog and edit the jump menu (Figure 24a).
Figure 24a. Opening the Jump Menu dialog by double-clicking Jump Menu in the Behaviors panel.
The Jump Menu dialog looks just like the Insert Jump Menu dialog, and you can add, remove, or move menu items or change menu options in this dialog.
Form data is collected using different kinds of form fields. Text is entered into text boxes or text areas. Options can be selected from sets of radio buttons. Data can be uploaded using file fields. Forms are submitted (or cleared) using Submit (or Reset) buttons.
To create a form in an open Web page in Dreamweaver, simply click to place the location of the form and choose Insert > Form > Form. The form displays as a dashed red box. The Properties inspector displays the form name.
Make sure you have clicked inside the form before you add any form fields (Figure 25a).
Figure 25a. A form placed on a page in Dreamweaver.
To activate prompts for accessibility options in forms, choose Edit > Preferences (Windows) or Dreamweaver > Preferences (Mac) and select the Accessibility category. Select the Form objects check box if it is not already selected. With form accessibility options activated, Dreamweaver prompts you with the Input Tag Accessibility Attributes dialog when you insert a form field into a form. The accessibility options allow visitors to fill out the form without using a mouse, or if they are relying on reader software, to have an identifying label read to them.
Form fields are used within a form to collect data. The main, most widely used field types are text boxes, drop-down menus, sets of radio (option) buttons, and check boxes.
Text fields are used to collect all kinds of information in a form. Email addresses, phone numbers, purchase prices, zip codes, names, and a wide variety of other data can be entered into text fields.
Text fields collect a single line of text. Text areas can collect multiple lines of text. Text areas are used to collect comments, descriptions (like descriptions of problems for online service forms), guestbook entries, and other text that requires more than one line.
To place a text field or a text area in a form, follow these steps:
Figure 26a. Defining a one-line text field.
As you define text field or text area attributes in the Properties inspector, they display in the Document window.
You can place any number of check boxes in a form. Check boxes provide two options for visitors: Checked or Unchecked. You can define a default state for a check box as either checked or unchecked.
To place a check box in a form, follow these steps:
Figure 26b. Defining a check box.
Radio buttons differ from check boxes in that they are always organized in groups. You never have a single radio button—if you are asking a question for which a user can supply no, one, or several answers, use check boxes. The purpose of radio buttons is to compel a user to choose one from a group of options.
To create a radio button group, follow these steps:
Figure 26c. Defining a group of radio buttons.
After you generate a radio button group, you can edit (or delete) radio buttons individually. If you want to add a radio button, you can copy and paste an existing one from the group and, in the Properties inspector, change the Checked value (but not the Radio Button) content.
Menus and file fields are two different types of fields that can be placed in forms. Menus allow visitors to choose from a list of items. File fields allow users to upload files when they submit a form. In this how-to, you’ll explore both types of fields. (Consider this two how-tos for the price of one—I had to sneak them both into the same one to keep the book at an even 100 how-tos!)
Menus (sometimes called pop-up menus) allow visitors to choose one option from a pop-up menu. The main difference between menus and list menus is that list menus allow users to select more than one choice from a list, whereas regular menus restrict users to choosing just one item. List menus are usually a confusing way to collect data and are rarely used.
To create a menu, follow these steps:
Figure 26d. Defining list values.
File field forms allow visitors to attach files from their own computers to the form and send them along with the form. You can allow visitors to attach files to the form submission by inserting a File field in a form. Choose Insert > Form > File Field. You can define character width in the Properties inspector. A Browse button appears next to the field that the user can use to navigate to and select a file to upload.
For form content to be sent to a server, there must be a Submit button in the form. Submit buttons are usually matched with a Reset button. The Reset button clears any data entered into the form and allows the user to start fresh.
To place a button in a form, choose Insert > Form > Button. Use the Properties inspector to define the button as a Submit or Reset button. In the Action area, choose the Submit form or Reset form radio button (Figure 26e).
Figure 26e. Defining a Submit button.
You can define custom labels for either the Submit or Reset button by entering text in the Value field for either button. Don’t get too fancy; visitors are used to seeing buttons that display something like Submit or Reset. But if you enter different text in the Value field, that text will display in browsers and can be previewed in the Document window.
Spry tools in Dreamweaver CS5 make it easy to define forms with attached validation scripts. These scripts test form content before it is submitted to make sure certain rules are met; specifically in the case of the new Spry form fields, they require that a visitor fill in a form field before submitting the form.
For example, if you want to require that visitors fill in the Email Address field in a form before they submit it, you can place a Spry Validation Text Field in your form. The four how-tos in this chapter explain how to use these Spry validation widgets.
All Spry validation widgets generate JavaScript to allow a form field to test input before processing the form. They also generate new CSS files that contain the formatting that defines the color, background color, text format, and so on for the form field and for form field input.
To place a Spry Validation Text Field widget in a form, follow these steps:
Figure 27a. Choosing Email Address as the validation type.
You can edit Spry validation rules at any time by selecting the turquoise Spry Text Field label and changing values in the Properties inspector.
Although validation scripts are most widely used for text boxes, they can be defined (in a way similar to the steps I just outlined for text boxes) for other form fields as well.
Form actions define how the data in a form is sent to a database on a server. Form actions are defined in the Properties inspector with the form—not any specific form field—selected.
To select a form, click the dashed red line defining the form border. Or, click the <form>
tag in the tag selector area on the bottom of the Document window.
The three important fields in the Properties inspector for a form are the Action, Method, and Enctype fields. What you enter in these fields is determined by how the programmer (who set up the script and database to which the form data is being sent) configured the database and scripts at the server. Normally, Method is usually set to POST but can sometimes be set to GET; this again depends on how data is transferred to the server and is defined by how the server is configured. The Action field contains the URL of the Web page at the server that has the script that will manage the data.
Enctype, short for “encryption type,” is sometimes used to define how characters are interpreted and formatted. Your server administrator will tell you what, if any, enctype coding is required for forms to be processed by your server.
Because form actions are determined by the settings at your server, the information you enter into the Properties inspector is provided by your server administrator. In the case of forms designed to match server scripts, those forms normally come with Action settings defined.
If you want to collect data in a form and have it sent to an email address, you can do this easily without having to work with additional server configuration or scripts. In the Action field, type mailto: < your email address>. From the Method pop-up menu, choose POST. In the Enctype field, type text/plain (Figure 28a).
Figure 28a. Defining an action that will send form content to an email address—in this case, mine!
Let me give you a recipe for one of the most widely used server-connected forms: a search engine. Follow these steps to place a FreeFind search field on an open Dreamweaver Web page. You can also use them as a model for using similar services.
Figure 28b. Placing a search field from FreeFind.
The form copied from FreeFind includes hidden fields, which are indicated by icons in the form. These fields have information that directs search queries to the index FreeFind prepares for your particular site.
Test your search field in a browser. The search field form has fields and buttons. You can reformat the fields and buttons using the techniques for defining form and form field attributes we’ve covered. In other words, you can customize the way this form looks—you just can’t delete any of the form fields. Other techniques in this chapter cover how customization works, so you can customize forms you get from CGI hosts.
52.15.148.187