Rick has got it totally right. One things to keep in mind
about <cfselect>:
- 'selected' attribute references the VALUE of the cfselect
option, not displayed text.
Thus, it must reference one or more valid items from the
query column defined in the 'value' attribute of cfselect tag. If
your query used in your <cfselect> returns id and title (i.e.
1-apples; 2-oranges), and <cfselect>'s 'value' attribute is
set to 'id' query column, then the value of 'selected' attribute
must be 1 or 2 (or both, comma-separated, for multiple select
lists), and NOT apples or oranges.
In many, if not most, cases your <cfselect>'s 'value'
attribute will be set to the 'id' column of your query, since you
are likely passing the selections made in your <cfselect> as
parameters to the form's action page or insert them into another
table in the database. And in most cases you can not be 100% sure
of current 'id' of the item(s) you want pre-selected in your
<cfselect>, especially if the table queried to populate your
<cfselect> is user-editable (i.e. user can delete the
existing 'apples' item from that database table, and then add a new
'apples' item, which will have a new id).
In such cases it may be better, instead of using
<cfselect>, to use a plain html <select> and loop
through the query to output <option> tags for it with 'id' as
value and 'title' as displayed text. You can then use a
<cfif>/<cfelse> conditional construct to assign a
selected state to a particular <option> tag based on the
tag's displayed text ('title' query column), not its value ('id'
query column).
Example:
<cfquery name="getFruits" datasource="dsn">
SELECT id, title FROM fruits ORDER BY id;
</cfquery>
let's say the query returns 3 rows (id-title): 1-apples,
2-oranges, 3-peaches.
you can then use
<select name="fruit_id" size="1">
<cfoutput query="getFruits">
<cfif getFruit.title is "Oranges">
<option value="#id#" selected>#title#</option>
<cfelse>
<option value="#id#">#title#</option>
</cfif>
</cfoutput>
</select>
The returned value of your selection in this <select>
tag will be the selected fruit's id (i.e. #form.fruit_id# will
return 2 in the above example based on default selection), but you
are pre-selecting an item in the select list based on the fruit's
title.