566
Chapter 26: Retrieving and Formatting Data
Reviewing the code
The following table describes the highlighted code and its function:
Testing for a variable's existence
Before relying on a variable’s existence in an application page, you can test to see if it exists using
the ColdFusion
IsDefined
function. A
function
is a named procedure that takes input and
operates on it. For example, the
IsDefined
function determines whether a variable exists. CFML
provides a large number of functions, which are documented in
CFML Reference
.
The following code prevents the error 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>
The argument passed to the
IsDefined
function must always be enclosed in double quotation
marks. For more information on the
IsDefined
function, see
CFML Reference
.
If you attempt to evaluate a variable that you did not define, ColdFusion cannot process the page
and displays an error message. To help diagnose such problems, turn on debugging in the
ColdFusion MX Administrator. The Administrator debugging information shows which variables
are being passed to your application pages.
Requiring users to enter values in form fields
One of the limitations of HTML forms is the inability to define input fields as required. Because
this is a particularly important requirement for database applications, ColdFusion provides a
server-side mechanism for requiring users to enter data in fields.
Code
Description
<cfquery name="GetEmployees"
datasource="CompanyInfo">
Queries the data source CompanyInfo and names the
query GetEmployees.
SELECT FirstName, LastName, Salary
FROM Employee
WHERE LastName=<cfqueryparam
value="#Form.LastName#"
CFSQLType="CF_SQL_VARCHAR">
Retrieves 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">
Displays results of the GetEmployees query.
#FirstName#
#LastName#
#Salary#<br>
Displays the value of the FirstName, LastName, and
Salary fields for a record, starting with the first record,
then goes to the next line. Keeps 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>
Closes the
cfoutput
block.
<br>
<cfoutput>Contractor: #Form.Contractor#
</cfoutput>
Displays 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.
Summary of Contents for COLDFUSION MX 61-DEVELOPING COLDFUSION MX
Page 1: ...Developing ColdFusion MX Applications...
Page 22: ...22 Contents...
Page 38: ......
Page 52: ...52 Chapter 2 Elements of CFML...
Page 162: ......
Page 218: ...218 Chapter 10 Writing and Calling User Defined Functions...
Page 250: ...250 Chapter 11 Building and Using ColdFusion Components...
Page 264: ...264 Chapter 12 Building Custom CFXAPI Tags...
Page 266: ......
Page 314: ...314 Chapter 14 Handling Errors...
Page 344: ...344 Chapter 15 Using Persistent Data and Locking...
Page 349: ...About user security 349...
Page 357: ...Security scenarios 357...
Page 370: ...370 Chapter 16 Securing Applications...
Page 388: ...388 Chapter 17 Developing Globalized Applications...
Page 408: ...408 Chapter 18 Debugging and Troubleshooting Applications...
Page 410: ......
Page 426: ...426 Chapter 19 Introduction to Databases and SQL...
Page 476: ...476 Chapter 22 Using Query of Queries...
Page 534: ...534 Chapter 24 Building a Search Interface...
Page 556: ...556 Chapter 25 Using Verity Search Expressions...
Page 558: ......
Page 582: ...582 Chapter 26 Retrieving and Formatting Data...
Page 668: ......
Page 734: ...734 Chapter 32 Using Web Services...
Page 760: ...760 Chapter 33 Integrating J2EE and Java Elements in CFML Applications...
Page 786: ...786 Chapter 34 Integrating COM and CORBA Objects in CFML Applications...
Page 788: ......