Building Forms

HTML forms begin with an opening FORM tag and end with a closing FORM tag. The FORM tag accepts two major attributes that you will use to describe the form's associated action page and how to submit values to it.

Within the form block, you'll describe the form controls needed to gather and submit user input.

Note Note:Because forms are not ColdFusion-specific, the syntax and examples that follow provide you with just enough detail to get going with ColdFusion Express.

FORM tag syntax

<FORM ACTION="ActionPage" METHOD="Post">
    ...
</FORM>

Form control syntax

There are a variety of form controls types available. You choose form control input types based on the type of input the user should provide. For example:

This code creates a text contol:

<INPUT TYPE="Text" NAME="ControlName" SIZE="Value" MAXLENGTH="Value">

This code creates a series of radio buttons:

<INPUT TYPE="RadioButton" NAME="ControlName" VALUE="Value1">DisplayName1
<INPUT TYPE="RadioButton" NAME="ControlName" VALUE="Value2">DisplayName2
<INPUT TYPE="RadioButton" NAME="ControlName" VALUE="Value3">DisplayName3

This code creates a drop down select box:

<SELECT NAME="ControlName">
    <OPTION VALUE="Value1 ">DisplayName1
    <OPTION VALUE="Value2">DisplayName2
    <OPTION VALUE="Value3">DisplayName3
</SELECT>

This code creates a check box:

<INPUT TYPE="Checkbox" NAME="ControlName" VALUE="Yes|No">Yes

This code creates a reset button:

<INPUT TYPE="Reset" NAME="ControlName" VALUE="DisplayName">

This code creates a submit button:

<INPUT TYPE="Submit" NAME="ControlName" VALUE="DisplayName">

Click here to see form controls in action.

Form usage example

The code below creates a form for users to enter employee information. The comments explain the code in this context.

<HTML>
<HEAD>
<TITLE>Input form</TITLE>
</HEAD>
<BODY>
<!--- define the action page in the form tag. The form variables will 
pass to this page when the form is submitted --->

<FORM ACTION="ActionPage.cfm" METHOD="POST">
<!--- title the form--->
<H4>Employee Data Form</H4>
<!--- use P tags to format your form--->
<P>
    <!--- text field label--->
    Employee Last Name<BR>
    <!--- text field allows users to enter an employee's last name--->
    <INPUT TYPE="Text" NAME="LastName" size="20" maxlength="50">
</P>
<P>
    <!--- drop down select box allows users to select a department --->
    <!--- label the form control--->
    Department<BR>
    <SELECT NAME="Department">
        <OPTION VALUE="Training">Training</OPTION>
        <OPTION VALUE="Marketing">Marketing</OPTION>
        <OPTION VALUE="HR">HR</OPTION>
        <OPTION VALUE="Sales">Sales</OPTION>
    </SELECT>
</P>
<P>
    <!--- checkbox allows users to identify contract employees --->
    <!--- label the field--->
    Contract Employee?<BR>
    <INPUT TYPE="Checkbox" NAME="Contract" VALUE="Yes">Yes
</P>
<P>
    <!--- the submit button passes data to the action page specified in 
the opening form tag--->
    <INPUT TYPE="Submit" NAME="SubmitButton" VALUE="Submit Form">
</P>
<P>
    <!--- reset button resets the data on the form  --->
    <INPUT TYPE="Reset" NAME="Reset" VALUE="Reset Form Values">
</P>
<!--- end form --->
</FORM>
</BODY>
</HTML>
Note To create a form to accept user input:
  1. Create a new application page in HomeSite.
  2. Save the page as FormPage.CFM within the CFDOCS directory.
  3. Remember that CFDOCS resides under your web root directory.

  4. Title the page Chapter 7 Form Page.
  5. Add an opening FORM tag directly under the BODY tag:
  6. <FORM ACTION = "ActionPage.cfm" METHOD = "POST">
    
  7. Add a heading for the form page:
  8. <H4>Employee Data Form </H4>
    
  9. Add P tags to format the heading.
  10. Add a text input control so that users can enter a last name on the form:
  11. <INPUT TYPE = "Text" NAME = "LastName" SIZE = "20" MAXLENGTH = "50">
    
  12. Add a label above the form control to describe it on the form:
  13. Last Name <BR>
    
  14. Add a select box so that users can select a department:
  15. <SELECT NAME = "Department">
        <OPTION VALUE = "Training">Training</OPTION>
        <OPTION VALUE = "Marketing">Marketing</OPTION>
        <OPTION VALUE = "HR">HR</OPTION>
         <OPTION VALUE = "Sales">Sales</OPTION>   
    </SELECT>
    
  16. Add a label above the select box to describe it on the form:
  17. Department<BR>
    
  18. Add a checkbox so that users can identify contract employees:
  19. <INPUT TYPE="Checkbox" NAME="Contract" VALUE="Yes">Yes
    
  20. Add a label above the checkbox to describe it on the form:
  21. Contract employee?<BR>
    
  22. Add a submit form control to pass the data to the action page:
  23. <INPUT TYPE="Submit" NAME="SubmitButton" VALUE="Submit Form">
    
  24. Add a reset form control so that users can reset the data on the form page:
  25. <INPUT TYPE="Reset" NAME="ResetButton" VALUE="Reset Form Values">
    
  26. Add a closing FORM tag before the closing BODY tag to end the form:
  27. </FORM>
    
  28. Add P tags throughout the code to format the page.
  29. Save the page.
  30. View the form in a browser.
  31. The form appears in the browser.

    Remember that you need an action page in order to submit values; you will create one later in this chapter.

  32. Modify as needed.

Click here to see how the page should look at this time.

Click here to see the code behind the scenes.

Note Note:You will receive errors if you submit the form for processing at this time. You will learn about action pages and the characteristics of form variables later on in this chapter.