User Guide

Working with Action Pages 47
This time an error occurs because the check box does not pass a variable to the
action page.
Reviewing the code
The following table describes the highlighted code and its function:
Testing for a variable’s existence
Before relying on a variables existence in an application page, you can test to see if it
exists using the
IsDefined function. A function is a named procedure that takes
input and operates on it. For example, the
IsDefined function determines whether a
variable is defined. CFML provides a large number of functions, which are
documented in the CFML Reference.
The following code prevents the error that you saw in the previous example by
checking to see if the Contractor Form variable exists before using it:
<cfif IsDefined("Form.Contractor")>
<cfoutput>Contractor: #Form.Contractor#</cfoutput>
</cfif>
Code Description
<cfquery name="GetEmployees"
datasource="CompanyInfo">
Query the data source CompanyInfo and
name the query GetEmployees.
SELECT FirstName, LastName, Salary
FROM Employee
WHERE LastName=#Form.LastName#
Retrieve the FirstName, LastName, and
Salary fields from the Employee table, but
only if the value of the LastName field
matches what the user entered in the
LastName text box in the form on
formpage.cfm.
<cfoutput query="GetEmployees">
Display results of the GetEmployees query.
#FirstName#
#LastName#
#Salary#<BR>
Display the value of the FirstName,
LastName, and Salary fields for a record,
starting with the first record, then go to the
next line. Keep displaying the records that
match the criteria you specified in the
SELECT statement, followed by a line break,
until you run out of records.
</cfoutput>
Close the cfoutput block.
<br>
<cfoutput>Contractor:
#Form.Contractor#</cfoutput>
Display a blank line followed by the text
Contractor: and the value of the form
Contractor check box. A more complete
example would test to ensure the existence of
the variable and would use the variable in the
query.