Adobe ColdFusion 8

Multiple selection lists

A multiple-selection list box is defined by a select or cfselect tag with a multiple or multipe="yes" attribute and a size attribute value greater than 1. ColdFusion treats the result when a user selects multiple choices from a multiple-selection list box like the results of selecting multiple check boxes. The data made available to your page from any multiple-selection list box is a comma-delimited list of the entries selected by the user; for example, a list box could contain the four entries: Training, Marketing, HR, and Sales. If the user selects Marketing and Sales, the form field variable value is Marketing,Sales.

You can use multiple-selection lists to search a database in the same way that you use check boxes. The following sections describe how you can use different types of multiple-selection data values.

Handling numeric values

Suppose you want the user to select departments from a multiple-selection list box. The query retrieves detailed information on the selected department(s), as follows:

Select one or departments to get more information on:
<cfselect name="SelectDepts" multiple>
    <option value="1">Training
    <option value="2">Marketing
    <option value="3">HR
    <option value="4">Sales
</cfselect>

If the user selects the Marketing and Sales items, the value of the SelectDepts form field is 2,4. If this parameter is used in the following SQL statement:

SELECT *
    FROM Departmt
    WHERE Dept_ID IN (#form.SelectDepts#)

The following statement is sent to the database:

SELECT *
    FROM Departmt
    WHERE Dept_ID IN (2,4)

Handling string values

Suppose you want the user to select departments from a multiple-selection list box. The database search field is a string field. The query retrieves detailed information on the selected departments, as follows:

<cfselect name="SelectDepts" multiple>
    <option value="Training">Training
    <option value="Marketing">Marketing
    <option value="HR">HR
    <option value="Sales">Sales
</cfselect>

If the user selects the Marketing and Sales items, the SelectDepts form field value is Marketing,Sales.

Just as you did when using check boxes to search database fields containing string values, use the ColdFusion ListQualify function with multiple-selection list boxes:

SELECT * 
    FROM Departmt
    WHERE Dept_Name IN (#ListQualify(Form.SelectDepts,"'")#) 

The following statement is sent to the database:

SELECT *
    FROM Departmt
    WHERE Dept_Name IN ('Marketing','Sales')