User Guide
46 Chapter 4 Retrieving and Formatting Data
However, instead of putting the LastName directly in the SQL WHERE clause, you
can use the text that the user entered in the form for comparison:
<cfquery name="GetEmployees" datasource="CompanyInfo">
SELECT FirstName, LastName, Salary
FROM Employee
WHERE LastName="#Form.LastName#"
</cfquery>
For more information on Dynamic SQL, see “Dynamic SQL” on page 96.
Creating action pages
Use the following procedure to create an action page for cormpage.cfm.
To create an action page for the form:
1 Create a new application page in ColdFusion Studio.
2 Enter the following code:
<html>
<head>
<title>Retrieving Employee Data Based on Criteria from Form</title>
</head>
<body>
<cfquery name="GetEmployees" datasource="CompanyInfo">
SELECT FirstName, LastName, Salary
FROM Employee
WHERE LastName=’#Form.LastName#’
</cfquery>
<h4>Employee Data Based on Criteria from Form</h4>
<cfoutput query="GetEmployees">
#FirstName#
#LastName#
#Salary#<br>
</cfoutput>
<br>
<cfoutput>Contractor: #Form.Contractor#</cfoutput>
</body>
</html>
3 Save the page as actionpage.cfm within the myapps directory.
4View
formpage.cfm in your browser.
5 Enter data, for example, Smith, in the Last Name box and submit the form.
The browser displays a line with the first and last name and salary for each entry
in the database that match the name you typed, followed by a line with the text
“Contractor: Yes”
6 Click Return in your browser to redisplay the form.
7 Remove the check mark from the check box and submit the form again.