Building Database-Driven Form Elements


Earlier you saw that the form page didn't have any ColdFusion functions or tags in it. In this section, you'll see how to use ColdFusion to dynamically generate a SELECT (drop-down list) form control from a database query to display a list of optionsin this case, colors.

The first step in this process is to perform the query on the form page to grab the color information that you want displayed in the drop-down list:

  <cfquery name="GetColors"           datasource="MyDSN"> SELECT ColorID, ColorName FROM Colors ORDER BY ColorName </cfquery> 

After you have the query, you just need to loop over the query result to print the variables in place of the hard-coded elements:

  <select name="ColorID">    <cfoutput query="GetColors">      <option value="#ColorID#">#ColorName#</option>    </cfoutput> </select> 

TIP

If you find that the dynamically generated form elements are not being created properly, you can always run the page in your browser and view the source. That way, you can see the generated HTML code. Looking at it should help you figure out what is wrong.




Macromedia ColdFusion MX 7 Certified Developer Study Guide
Macromedia ColdFusion MX 7 Certified Developer Study Guide
ISBN: 0321330110
EAN: 2147483647
Year: 2004
Pages: 389
Authors: Ben Forta

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net