Chapter 5: Making Variables Dynamic
<OPTION VALUE="'Sales'">Sales
</SELECT>
<INPUT TYPE="hidden"
NAME="SelectDepts_required"
VALUE="You must select at least one department.">
If the user selected the Marketing and Sales items, the value of the SelectDepts form
field would be 'Marketing','Sales'.
Just as you did when using checkboxes to search database fields containing string
values, use the ColdFusion PreserveSingleQuotes function with multiple select boxes:
SELECT *
FROM Departments
WHERE DepartmentName IN (#PreserveSingleQuotes(form.SelectDepts)#)
The statement sent to the database would be:
SELECT *
FROM Departments
WHERE DepartmentName IN ('Marketing', 'Sales')
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 IsDefined function. For example, the following code checks to see if a
Form variable named Order_ID exists:
<CFIF Not IsDefined("FORM.Order_ID")>
<CFLOCATION URL="previous_page.cfm">
</CFIF>
The argument passed to the IsDefined function must always be enclosed in double
quotes. See the CFML Language Reference for more information on the IsDefined
function.
If you attempt to evaluate a variable that has not been defined, ColdFusion will not be
able to process the page. To help diagnose such problems, use the interactive debugger
in ColdFusion Studio or turn debugging on in the ColdFusion Administrator. The
Administrator debugging information shows which variables are being passed to your
application pages.
Creating Default Variables with CFPARAM
Another way to create a variable is to test for its existence and optionally supply a
default value if the variable does not already exist. The following shows the syntax of
the CFPARAM tag:
<CFPARAM NAME=" VariableName "
TYPE=" data_type "
DEFAULT=" DefaultValue ">
51
Need help?
Do you have a question about the COLDFUSION 4.5-DEVELOPING WEB and is the answer not in the manual?