Chapter 4: Form Handling using ColdFusion MX


Forms help end users interact with an application. An HTML form captures data and posts it to another template for processing. When an end user fills out and submits an HTML form, the Web server receives the data along with the name of the template that should process it. Regular HTML forms have limited input types and data-validation capabilities.

ColdFusion can extend the capabilities of regular HTML forms. It provides a set of tags that help in the validation and updating of data. The form handler contains CFML code that processes this data and sends an HTML document as output to the client. Usually, a Web browser displays a confirmation page that informs the end user that his data was processed.

Introduction to HTML Forms

HTML forms are created in two stages. The first stage creates the forms and the controls required for collecting data. The second stage involves the design of the form handler that processes the data collected by the form on the Web server.

The following code creates a form using the <FORM> tag:

 <HTML> <HEAD> <TITLE>Learning Forms 1</TITLE> </HEAD> <BODY> <FORM ACTION="forms2.cfm" METHOD="POST">     Enter your name and then click <B>Process</B>     <P>     First name:     <INPUT TYPE="text" NAME="FirstName">     <BR>     Last name:     <INPUT TYPE="text" NAME="LastName">     <BR>     <INPUT TYPE="submit" VALUE="Process"> </FORM> </BODY> </HTML> 

This code creates a form with two data entry fields and a Submit button. The <FORM> tag 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 submission of the form. To submit a form to ColdFusion, specify the name of the ColdFusion template that will process the form. This example specifies that the template forms2.cfm should process the submitted form:

 ACTION="forms2.cfm" 

The METHOD attribute specifies how data is to be sent back to the Web server. All ColdFusion forms must be submitted using the POST method.

Forms can contain data entry controls, which help the end user make choices and enter the correct data. These data entry controls include text boxes, password boxes, radio buttons, checkboxes, select boxes, text areas, image buttons, and hidden fields. In addition to the data entry controls, a Submit button enables you to submit the form data to the server.

Text Box Control

This control collects data such as a person's name or address. The syntax for a Text Box control is

 <INPUT TYPE="text" NAME="control_name"          SIZE="size" VALUE="default_value"> 

This control takes the following attributes:

  • The TYPE parameter is set to text.

  • The NAME attribute uniquely identifies the control.

  • The SIZE attribute specifies the number of characters that the control can accept.

  • The VALUE attribute is optional and specifies an initial value to be displayed in the box.

Password Text Box Control

When an end user types a password in the Password Text Box control, asterisks are displayed on the screen instead of password characters. The syntax for the Password Text Box control is

 <INPUT TYPE="password" NAME="control_name"         SIZE="size"> 

The TYPE parameter for the Password Text Box control is set to "password". The NAME attribute uniquely identifies the control, while the SIZE attribute specifies the number of characters the control can accept.

Button Controls

Most forms have at least one Submit button, which the end user can click to submit the form and its contents to the server. The syntax for the Submit Button control is

 <INPUT TYPE="submit"          VALUE="caption" NAME="name"> 

The TYPE parameter is set to "submit". The NAME parameter is optional. The VALUE parameter is displayed as the caption on the button.

The Reset button clears form fields. The syntax for the Reset Button control is

 <INPUT TYPE="reset"          VALUE="caption> 




Macromedia ColdFusion MX. Professional Projects
ColdFusion MX Professional Projects
ISBN: 1592000126
EAN: 2147483647
Year: 2002
Pages: 200

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