Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I dynamically populate a <select> drop-down?

Dynamic Population

How do I dynamically populate a <select> drop-down?

by  simmerdown  Posted    (Edited  )
one)
Query the database to get the list of items for display in the <select> dropdown. If you'll be using the <select> to pass values (which is usually the case) make sure you're also getting the appropriate values.

[color 003366]<cfquery datasource="#datasource# name="list">
SELECT Item, Value
FROM ItemTable
ORDER BY Item
</cfquery>
[/color]

two)
Code the <select> box to fill with your query data. The usual parts of

[color 003366]<form name="item_search">[/color]

and

[color 003366]<select name="ItemMenu">[/color]

come first, as well as the optional static first item:

[color 003366] <option value="0">items:</option>[/color]

Then use <cfoutput> to loop through the results of the query, adding another <option> for each record returned.

[color 990000] <cfoutput query="list">
<option value="#Value#">#Item#</option>
</cfoutput>
[/color]

Close off the <select> box (and immediately or eventually) the form,

[color 003366] </select>
</form>
[/color]

and you're done.

"Run that past me again, all at once?"

[color 003366]<cfquery datasource="#datasource# name="list">
SELECT Item, Value
FROM ItemTable
ORDER BY Item
</cfquery>

<form name="item_search">
<select name="ItemMenu">
<option value="0">items:</option>
[color 990000]<cfoutput query="list">
<option value="#Value#">#Item#</option>
</cfoutput>[/color]
</select>
</form>
[/color]

This technique can easily be applied to generating ordered and unordered html lists, populating table rows, etc.

Happy generating.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top