Populate a dropdown selectbox from DB

Dynamically populate a dropdown select from a database with unique data.

<!---
Select ID & Brand using Distinct to make sure there are no
duplicates in the dropdown
--->
<cfquery datasource="Examples" name="DB_">
Select Distinct ID,Brand from Computers Order By Brand
</cfquery>

<!---
Output the results from the query into the dropdown
--->
<select name="Computer">
  <cfoutput query="DB_">
    <option value="#ID#">#Brand#</option>
  </cfoutput>
</select>

The result is a dropdown with the Brand names shown alphabetically ..
and the value of the selection being passed to the next page is the ID
of the Brand.

 

Share this article!

Follow us!

Find more helpful articles: