During the last procedure, you hard-coded a form's select box options.
Instead of manually entering department information on a form, you can dynamically populate a select box with database column fields. When you code this way, changes that you make to a database are automatically reflected on the form page.
To dynamically populate a select box:
This code retrieves department data for a form page and stores it in the GetDepartments query variable:
<CFQUERY NAME="GetDepartments" DATASOURCE="HRApp"> SELECT Department_Name FROM Departments </CFQUERY>
This code dynamically populates the Department_Name select box with the GetDepartments query data:
<SELECT NAME="Department_Name"> <OPTION VALUE="All">All</OPTION> <CFOUTPUT QUERY="GetDepartments"> <OPTION VALUE="#Department_Name#"> #Department_Name# </OPTION> </CFOUTPUT> </SELECT>
![]() |
To dynamically populate a select box: |
FormPage.cfm
in HomeSite.<CFQUERY NAME="GetDepartments" DATASOURCE="HRExpress"> SELECT Department_Name FROM Departments </CFQUERY>
<SELECT NAME="Department_Name">
<OPTION VALUE="All">All</OPTION>
<CFOUTPUT QUERY="GetDepartments"> <OPTION VALUE="#Department_Name#"> #Department_Name# </OPTION> </CFOUTPUT>
FormPage.cfm
in a browser. The changes that you just made appear in the form.
Remember that you need an action page to submit values.
Click here to see how the page should look at this time.
Click here to see the code behind the scenes.
Move on in this chapter to learn how to create action pages and work with form variables.
![]() |
Note: | The behind the scenes code refers to a different action page than the one that you are building. |
You will receive errors if you submit the form for processing at this time. You will learn about action pages and the characteristics of form variables later on in this chapter.