User Guide

Table Of Contents
Dynamically populating list boxes 625
Dynamically populating list boxes
The code in “Creating a basic form” on page 611 hard-coded the forms list box options. Instead
of manually entering the information on a form, you can dynamically populate a list box with
database fields. When you write code this way, the form page automatically reflects the changes
that you make to the database.
You use two tags to dynamically populate a list box:
Use the cfquery tag to retrieve the column data from a database table.
Use the cfoutput tag with the query attribute within the select tag to dynamically populate
the
options of this form control.
To dynamically populate a list box:
1.
Open the formpage.cfm page.
2.
Modify the file so that it appears as follows:
<html>
<head>
<title>Input form</title>
</head>
<body>
<cfquery name="GetDepartments" datasource="cfdocexamples">
SELECT DISTINCT Location
FROM Departmt
</cfquery>
<!--- Define the action page in the form tag.
The form variables pass to this page
when the form is submitted --->
<cfform action="actionpage.cfm" method="post">
<!--- Text box. --->
<p>
<cfloop index="randomindex" from="1"
to="200000" step="1">
<cfset random=Rand()>
</cfloop>
Inserts an artificial delay by using the Rand function
to calculate many random numbers.
<cfloop index="Myindex" from="1"
to="10" step="1">
<cfloop index="randomindex"
from="1" to="100000" step="1">
<cfset random=rand()>
</cfloop>
<cfoutput>
Magic number #Myindex#
is:&nbsp;&nbsp;#RandRange
(100000,999999)#<br><br>
</cfoutput>
</cfloop>
Generates and displays 10 random numbers. This
code uses two loops. The outer loop repeats ten
times, once for each number to display. The inner
loop uses the
Rand function to create another delay
by generating more (unused) random numbers. It
then calls the
RandRange function to generate a six-
digit random number for display.
Code Description