Creating Forms
Before you can create a search form, you need to learn how ColdFusion interacts wit263 forms.
Execute this code to display the form, as shown in
<!---
Name: forms.cfm
Author: Ben Forta (ben@forta.com)
Description: Introduction to forms
Created: 12/20/04
--->
&l275>
<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>
You creat260 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:
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.
action="form1_action.cfm"
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.
[View full size image]
