User Guide

Table Of Contents
Creating dynamic check boxes and multiple-selection list boxes 627
Reviewing the code
The following table describes the highlighted code and its function:
Creating dynamic check boxes and multiple-selection list boxes
When an HTML or CFML form contains a list of check boxes with the same name or a multiple-
selection list box (that is, a box in which users can select multiple items from the list), the users
entries are made available as a comma-delimited list with the selected values. These lists can be
very useful for a wide range of input types.
Note: If the user does not select a check box or make a selection from a list box, no variable is
created. The
cfinsert and cfupdate tags do not work correctly if there are no values. To prevent
errors, make the form fields required, use dynamic SQL, or use the
cfparam tag to set a default value
for the form field.
Check boxes
When you put a series of check boxes with the same name in a form, the variable that is created
contains a comma-delimited list of values. The values can be either numeric values or
alphanumeric strings. These two types of values are treated slightly differently.
Handling numeric values
Suppose you want a user to select one or more departments using check boxes. You then query the
database to retrieve detailed information on the selected department(s). The code for a simple set
of check boxes that lets the user select departments looks like the following:
<cfinput type="checkbox"
name="SelectedDepts"
value="1">
Training<br>
<cfinput type="checkbox"
Code Description
<cfquery name="GetDepartments"
datasource="cfdocexamples">
SELECT DISTINCT Location
FROM Departmt
</cfquery>
Gets the locations of all departments in the
Departmt table. The DISTINCT clause eliminates
duplicate location names from the returned query
results.
<cfset optsize=getDepartments.recordcount
+ 1>
Sets the optsize variable to the number of entries to
add dynamically to the selection list, plus one for the
manually coded Select All option.
<cfselect name="City"
query="GetDepartments" value="Location"
size="#optsize#">
<option value="">Select All
</cfselect>
Populates the City selection list from the Location
column of the GetDepartments query. The control
has one option for each row returned by the query.
Adds an option that allows users to select all
locations. If the user selects this option, the form
value is an empty string. The action page must
check for the empty string and handle it
appropriately.