Creating Forms


Before you can create a search form, you need to learn how ColdFusion interacts with HTML forms. Listing 12.1 contains the code for a sample form that prompts for a first and last name. Create this template, then save it in a new folder named 12 (under the application root) as form1.cfm.

TIP

As a reminder, the files created in this chapter are in directory 12, so use that in your URLs too.


Listing 12.1. form1.cfmHTML Forms
 <!--- Name:        forms.cfm Author:      Ben Forta (ben@forta.com) Description: Introduction to forms  Created:     12/20/04 ---> <html> <head>  <title>Learning ColdFusion Forms 1</title> </head> <body> <!--- Movie search form ---> <form action="form1_action.cfm" method="POST"> Please enter the movie name and then click <strong>Process</strong>. <p> Movie: <input type="text" name="MovieTitle"> <br> <input type="submit" value="Process"> </form> </body> </html> 

Execute this code to display the form, as shown in Figure 12.1.

Figure 12.1. You can use HTML forms to collect data to be submitted to ColdFusion.


This form is simple, with a single-data entry field and a submit button, but it helps clearly demonstrate how forms are used to submit data to ColdFusion.

Using HTML Form Tags

You create HTML forms by using the <form> tag. <form> usually takes two parameters passed as tag attributes. The action attribute specifies the name of the script or program that the Web server should execute in response to the form's submission. To submit a form to ColdFusion, you specify the name of the ColdFusion template that will process the form. The following example specifies that the template form1_action.cfm should process the submitted form:

 action="form1_action.cfm" 

The method attribute specifies how data is sent back to the Web server. As a rule, all ColdFusion forms should be submitted as type post.

CAUTION

The default submission type is not post; it is usually get. If you omit the method="post" attribute from your form tag, you run the risk of losing form data, particularly in long forms or forms with textarea controls.


Your form has only a single data entry field: <input type="text" name="MovieTitle">. This is a simple text field. The name attribute in the <input> tag specifies the name of the field, and ColdFusion uses this name to refer to the field when it is processed.

Each field in a form is usually given a unique name. If two fields have the same name, both sets of values are returned to be processed and are separated by a comma. You usually want to be able to validate and manipulate each field individually, so each field should have its own name. The notable exceptions are the check box and radio button input types, which we'll describe shortly.

The last item in the form is an <input> of type submit. The submit <input> type creates a button that, when clicked, submits the form contents to the Web server for processing. Almost every form has a submit button (or a graphic image that acts like a submit button). The value attribute specifies the text to display within the button, so <input type="submit" value="Process"> creates a submit button with the text Process in it.

TIP

When you're using an input type of submit, you should always specify button text by using the value attribute. If you don't, the default text Submit Query (or something similar) is displayed, which is likely to confuse your users.


Form Submission Error Messages

If you enter a movie title into the field and submit the form right now, you will receive a ColdFusion error message like the one in Figure 12.2. This error says that file form1_action.cfm can't be found.

Figure 12.2. ColdFusion returns an error message when it can't process your request.


This error message is perfectly valid, of course. You submitted a form to be passed to ColdFusion and processed it with a template, but you haven't created that template yet. So your next task is to create a template to process the form submission.



Macromedia Coldfusion MX 7 Web Application Construction Kit
Macromedia Coldfusion MX 7 Web Application Construction Kit
ISBN: 321223675
EAN: N/A
Year: 2006
Pages: 282

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net