The School for Champions is an educational website that shows you how to achieve your dreams.
![]() |
|
|
SfC Home > ColdFusion > Explanation of how to use drop-down lists or menus with the ColdFusion markup language. Also refer to forms, database, query, SQL, output, code, Ron Kurtus, School for Champions. Copyright © Restrictions Using Drop-Down Lists in ColdFusionby Ron Kurtus (15 March 2001) Drop-down lists or menus are often used to facilitate selections when entering data. Programming those lists in ColdFusion can be tricky, especially when you are using data from multiple tables in the database. Questions you may have include:
This lesson will answer those questions. There is a mini-quiz near the end of the lesson. Standard drop-down listA standard drop-down list or menu, as part of a form, looks like: The HTML code for this is: <FORM METHOD="POST" ACTION="nextpage.cfm"> FORM... ACTION is the page where you will send the results of your selection. SELECT... NAME is the name of the variable being posted or the record or column of the table in the database. OPTION... VALUE is the value of the variable being sent. For example, Apples could be inserted in the Fruits column. ColdFusion drop-down listIf these items are in a database, you can use ColdFusion to display them, instead of writing each out individually. Obviously, the page must be a .CFM page instead of .HTM or .HTML. To do this, first you must make a query: <CFQUERY NAME="list" DATASOURCE="plants"> Then the output is stated within the form: <FORM METHOD="POST" ACTION="nextpage.cfm"> Note that the list comes from the FruitList table, but the selection will be going to the Fruits database table. Location of <CFOUTPUT>Note the location of <CFOUTPUT>. If it is located anywhere else, you will get unusual results. Clicking button without selectingNow, a problem can occur if the user would click the Submit Fruit button without making a selection, especially if the result is to be input into a database. One solution is to set the VALUE of that option to some default. Drop-down for editingSuppose you had a page to edit a record, using a drop-down list. In such a case, you would have to have two queries and two outputs. The first query selects a specific fruit, sent through a form from the previous page. <CFQUERY NAME="item" DATASOURCE="plants"> The second query selects all fruits from the FruitList table. <CFQUERY NAME="list" DATASOURCE="plants"> The first output shows the selection and gives its value, in case no new selection is made. Then the second output finishes the drop-down list with all the choices from the query. <FORM METHOD="POST" ACTION="nextpage.cfm"> SummaryYou can use a standard drop-down list to send variables to a ColdFusion page to insert or update a database. You can populate the list, from a ColdFusion query. When editing a database field or column, you can show the present value and provide a list for a new selection. Two queries are used from different tables. Congratulate yourself when you do good programming ResourcesThe following resources provide information on this subject. WebsitesBooksMiscellaneousMini-quiz to check your understanding1. Why would you use a drop-down list? 2. Why must you query before populating a drop-down menu? 3. Why would you use two queries for a drop-down list in an editing page? If you got all three correct, you are on your way to becoming a Champion in ColdFusion Development. If you had problems, you had better look over the material again. What do you think?Do you have any questions, comments, or opinions on this subject? If so, send an email with your feedback. We will try to get back to you as soon as possible. Share linkFeel free to establish a link from your website to pages in this site. Or use our form to send this link to yourself or a friend. Students and researchersThe Web address of this page is Where can you go from here?
|
The School for Champions helps you become the type of person that can be can be called a Champion.