A Selection Control

The cfselect tag extends the functionality provided by the HTML select tag by including data validation as well as a mechanism for quickly and easily building dynamic drop-down boxes from query results. Example 10-6 shows a simple use of cfselect with static option values.

Example 10-6. Using cfselect with static option values

<cfif IsDefined('form.Selectbox')>
  <cfoutput>You submitted <b>#form.Selectbox#</b>.</cfoutput>
  <hr noshade>
</cfif>
   
<cfform action="#CGI.Script_Name#" name="MyForm" 
enctype="application/x-www-form-urlencoded">
   
<cfselect name="Selectbox" size="5" 
          message="You must select at least one item from the selection box." 
          required="Yes" multiple="Yes">
   <option value="January">January</option>
   <option value="February">February</option>
   <option value="March">March</option>
   <option value="April">April</option>
   <option value="May">May</option>
   <option value="June">June</option>
   <option value="July">July</option>
   <option value="August">August</option>
   <option value="September">September</option>
   <option value="October">October</option>
   <option value="November">November</option>
   <option value="December">December</option>
</cfselect>
<p>    
<input type="submit" name="submit" value="Submit Form">               
</cfform>

In Example 10-6, a drop-down box is created within a cfform using the cfselect tag. We specify the name of the select-box field using the name attribute. Setting the size to 5 creates a select box five rows high. Note that size must be set to at least ...

Get Programming ColdFusion MX, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.