Building or creating dynamic drop down

Posted on 2011-03-04
Medium Priority
Last Modified: 2012-06-22

I need to build a dynamic dropdown list based on the selection of a other dropdown. Also I don't want to submit
the form since I have other data entry field on my form and don't want to lose the user entered data.
What is the best way to achive it using CF and/or Java Script. An example would really help to save the time since it is kind of urgent.

My first query is : select state from mydatabse.states
My second query is : select counties from mydatabase.counties where state = #state#

Thanks in Advance
Question by:Tpaul_10
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2

Author Comment

ID: 35040993
Thanks for the quick reply and example srikanthmadishetti:
Right now I am not using or don't want to use the CFC. So, can I just use the fuction in that example and build my second drop down in the same page? with regular HTML instead of CFSELCT etc.?
LVL 39

Accepted Solution

gdemaria earned 1800 total points
ID: 35041597


a CF custom tag that creates the javascript for you..

All you do is write a query that has a join between both items you want to display.  For example,  a query "getProducts" is a list of products and the options available to that product (let's say SIZE : small, medium...)

Then you just fill in the fields :
   Name - the form's field name
   Display - the column that will be show to the user in the select tag
   Value - the column that will be the value of the select tag
   emptytext - what should appear when nothing is selected
   selected - the value of the field that should be shown when loaded
   formName - the name of the form the select tags are put into
   htmlBetween - any HTML that you want to appear between the two select tags, for example, maybe they are columns in a table so you would put </td><td> between the tags

<cf_twoselectsrelated query="getProducts"
   emptyText1="- Select a Product -"
   emptyText2="- Select a Size"

Open in new window

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

LVL 11

Assisted Solution

by:Brijesh Chauhan
Brijesh Chauhan earned 200 total points
ID: 35043623
>>Right now I am not using or don't want to use the CFC. So, can I just use the fuction in that example and build my second drop down in the same page? with regular HTML instead of CFSELCT etc.?

You can bind it to a CFM.. try below example

your form.cfm

<cfform name="UserForm" method="post" format="html">
  <select name="CategorySearch">
  	<option value="1">Test1</option>
    <option value="2">Test2</option>
   <cfselect width="50" name="SubCategories" bind="url:test.cfm?keyword={CategorySearch}" display="name" value="Aid" />

Open in new window

Create a file test.cfm which will have the query to pull the information for second select

   <cfset Category = queryNew("Aid, name")>
   <cfset queryAddRow(Category,1)>
   <cfset querySetCell(Category,'Aid',1)>
   <cfset querySetCell(Category,'name','Test1')>
   <cfset queryAddRow(Category,1)>
   <cfset querySetCell(Category,'Aid',2)>
   <cfset querySetCell(Category,'name','Test2')>

<cfquery name="getCatregory" dbtype="query">
	select Aid, name
    from Category
    where Aid = #url.keyword#


Open in new window

LVL 13

Expert Comment

ID: 35044232
@brijesh Good to see you in EE :)

>>>Right now I am not using or don't want to use the CFC. So, can I just use the fuction in that example and build my second drop down in the same page? with regular HTML instead of CFSELCT etc.?

Is there any specific reason for not using the cfselect or cfc

First it will be just few lines of code and second cfc will help you to reuse this code any where .

You can use it any form which has the state and country drop downs .

LVL 16

Expert Comment

by:Gurpreet Singh Randhawa
ID: 35054410

Author Comment

ID: 35079795
I have old cold fusion code and the site is going away soon. So, no CFCs and don't want to spend money for buying the code -:(

I may look in to writing a custom tag, but need to find out if the tables are related to have one query.
May be an example would be helpful.

LVL 39

Expert Comment

ID: 35083361
I provided you with a custom tag:   cf_twoselectsrelated


> but need to find out if the tables are related to have one query

The would have to be related, wouldn't they?   Otherwise, you don't need two related select tags, you could just do two distinctly seperate tags, right?


Author Comment

ID: 35225709
Sorry about the delay and Thanks for all your inputs.
I was able to created a custom tag successfully with the exaple provided by gdemaria. THANKS.

now the problem is , if I go bak and edit my order, I should be able to pre-select the options which I have selected originally like following.
<option value="#myvalue#" <cfif #ListFind(preselectedValue,query2.value1)#> #SELECTED</cfif>>#myvalue#</option>                   

I am going to try it by adding another parameters to my customtag and use the similar condition like above. But if you have a better solution, please update me.

LVL 39

Expert Comment

ID: 35226667
the custom tag has two parameters selected1 and selected2 representing the current values of the two select tags

Author Comment

ID: 35285907
THANKS gdemaria.
Based on my requirements, your answer gave me the complete solution eventhough others have provided with different soluctions

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article  is about submitting  form through  ColdFusion.Ajax.submitForm to the action page and send a response back in JSON format which later can be decoded using ColdFusion.JSON.decode. By this way you can avoid the usual page refresh for subm…
Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question