Here is another EE topic with the same topic in the coldfusin forum.
http://www.experts-exchang
Main Topics
Browse All TopicsHi,
I have two tables, one is countries and the other is counties.
I need the first drop down box to list all the countries, which it does :) The second one needs to be dynamically populated depending on the results of the first one. Thing is I want to do it without a page reload and just cant get it to work :(
<cfquery NAME="pull_country" DATASOURCE="#application.a
SELECT country_id, country
FROM countries
ORDER BY country
</cfquery>
<cfquery NAME="pull_locations" DATASOURCE="#application.a
SELECT county_id, county
FROM counties
ORDER BY county
</cfquery>
<select name="country" class="formobjects" id="location">
<option value="0">Please Select</option>
<cfoutput QUERY="pull_country">
<option value="#country_id#">#coun
</cfoutput>
</select>
Any suggestions?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Here is another EE topic with the same topic in the coldfusin forum.
http://www.experts-exchang
Sorry, the CF Link actually has links to three methods. None of these are trivial.
Here is the Javascript method:
http://www.experts-exchang
--- it actually has links to CF custom tag code also that should simplify this, but I didn't dig into it...
Here is a link to a method drawn up by mrichmon that shows how to submit queries in the background to dynamically change the lists
http://www.experts-exchang
The last method that is mentioned is to use CFAJAX, but they don't really provide details on how to do this specifically.
http://www.indiankey.com/c
I believe it is related to this method. http://www.indiankey.com/c
What you are wanting is not trivial and is going to take a lot of code. It doen't make sense to try and paste all that code into another link and the best that is going to happen is for someone just to copy code from one of these other links...
I wrote this a while ago so it's not pretty, but it got the job done. I modified it a little to try and fit your request so forgive any typos :-)
<cfset CountyQry = QueryNew("CountyID,County_
<cfoutput query="counties">
<CFSET QueryAddRow(CountyQry,coun
<Cfset QuerySetCell(CountyQry,"Co
<Cfset QuerySetCell(CountyQry,"Co
<Cfset QuerySetCell(CountyQry,"Co
</cfoutput>
<script type="text/javascript">
function chgcounty(SelCountry){
var SelCounty;
var SelCountry;
SelCounty = document.myformname.counti
<cfloop query="pull_country">
if(SelCountry == "#pull_country.country_id#
SelCounty.options.length = countyQry.recordcount#+1;
<Cfquery dbtype="query" name="cnty">
SELECT CountyID,County_name,Count
FROM CountyQry
WHERE CountryID = #pull_country.country_id#
ORDER BY County_Name
</CFQUERY>
<cfloop query="cnty">
SelCounty.options[cnty.cur
</cfloop>
}
</cfloop>
}
</script>
<select name="countries" onchange="chgcounty(this.o
<cfloop query="pull_country">
<option value="#pull_country.id#">
</cfloop>
</select>
<select name="counties">
<option value="">
</select>
I took some liberties, like assuming your County DB table has the country ID, assuming the name of your FORM, etc...
May need a little configuring on your side but should help.
Good luck
Oops, just noticed my own typos...
First, the whole of the JAvaScript should be in a set of CFOUTPUT tags and this line:
SelCounty.options[cnty.cur
should be:
SelCounty.options[#cnty.cu
Good luck
I would suggest the tried and tested cf_twoselectsrelated - you can see it in action at jobs.manpower.com.au
You can get the code at http://projects.nateweiss.
I'm trying to use your code but I get this error ...
Error Occurred While Processing Request
There is no column in the query object.
Here's the code:
<cfquery NAME="pull_country" DATASOURCE="#application.a
SELECT country_id, country
FROM countries
ORDER BY country
</cfquery>
<cfquery NAME="pull_locations" DATASOURCE="#application.a
SELECT county_id, county, country_id
FROM counties
ORDER BY county
</cfquery>
<cfset CountyQry = QueryNew("county_id,county
<cfoutput query="pull_locations">
<CFSET QueryAddRow(CountyQry,pull
<Cfset QuerySetCell(CountyQry,"Co
<Cfset QuerySetCell(CountyQry,"Co
<Cfset QuerySetCell(CountyQry,"Co
</cfoutput>
<CFOUTPUT>
<script type="text/javascript">
function chgcounty(SelCountry){
var SelCounty;
var SelCountry;
SelCounty = document.myformname.counti
<cfloop query="pull_country">
if(SelCountry == "#pull_country.country_id#
SelCounty.options.length = #countyQry.recordcount#+1;
<Cfquery dbtype="query" name="cnty">
SELECT CountyID,County_name,Count
FROM CountyQry
WHERE CountryID = #pull_country.country_id#
ORDER BY County_Name
</CFQUERY>
<cfloop query="cnty">
SelCounty.options[#cnty.cu
</cfloop>
}
</cfloop>
}
</script>
<select name="countries" onchange="chgcounty(this.o
<cfloop query="pull_country">
<option value="#pull_country.count
</cfloop>
</select>
<select name="counties">
<option value="">
</select>
</CFOUTPUT>
Take a look at the tag that mmc98dl1 suggests - Nate Weiss's two selects related is a very clean simple solution to this problem.
All you need do is write a JOINed query of your two tables, suitably GROUPed and pass it into Nate's tag. The attributes on the tag allow you to simply chose which type of select you need as well as any other details you need to set for the selects.
If you want to write this yourself, use the CFWDDX tag to get from query from CF into an array in JS.
This JS function should help you populate any given select.
<script language='JavaScript' type="text/javascript">
function fillnextdropdown(default_v
// with == in the form
with (eval("document."+formname
if (selected_value != 0) {
// If a real selection has been made
// Set up the defaults and clear the Activity Year Options
NewOpt = new Option;
var OptionCount = 0;
thisField = eval(fieldname);
thisQuery = eval(queryname);
thisParentIDCol = eval("thisQuery."+ParentID
thisChildIDCol = eval("thisQuery."+ChildID)
thisdspColumn = eval("thisQuery."+dspColum
RowCount = thisParentIDCol.length;
thisField.options.length = 0;
// Add in a new top option
NewOpt.value = 0;
NewOpt.text = default_text;
thisField.options[OptionCo
// Now loop through the query we converted to a couple of arrays and output the appropriate options
for (var i = 0; i < RowCount; i++){
if (thisParentIDCol[i] == selected_value){
OptionCount++;
NewOpt = new Option;
NewOpt.value = thisChildIDCol[i];
NewOpt.text = thisdspColumn[i];
thisField.options[OptionCo
if (NewOpt.value == default_value)
thisField.selectedIndex = OptionCount;
}
}
}
}
}
</script>
Here is an example of a call of this function.
<select name="SubGroupID" class="formstyleblue" onchange="fillnextdropdown
<option value="0" class="bodytext">Select a Template Subscription Group >></option>
<cfoutput query="SubGroupTemplates" group="SubGroupID">
<option value="#SubGroupTemplates.
</cfoutput>
</select>
<select name="TemplateID" class="formstyleblue">
<option value="0">Select a Subscription Group from the list above</option>
</select>
These lines need to be included in the page to allow CFWDDX to create the JS array from the query for this example:
<script language='JavaScript' src='/cfide/scripts/wddx.j
<script language="JavaScript" type="text/javascript">
<cfwddx action="cfml2js" input="#SubGroupTemplates#
</script>
And this is the query that is used to populate the first select and the JS array
<cfquery name="SubGroupTemplates" datasource="#request.dsn#"
SELECT E.SubGroupID, E.Title, ET.TemplateID, CONCAT(ET.TemplateTitle,' (',ET.TemplateType,')') AS TemplateTitle
FROM EmailSubGroup E
INNER JOIN EmailTemplate ET ON E.SubGroupID=ET.SubGroupID
ORDER BY E.Title, ET.TemplateTitle
</cfquery>
Business Accounts
Answer for Membership
by: RCorfmanPosted on 2006-03-25 at 14:29:17ID: 16290973
Here is a link to the same question, but with and ASP developer... e.com/Web/ Web_Langua ges/ASP/ Q_ 20996834.h tml e.com/Prog ramming/ Pr ogramming_ Languages/ C_Sharp/Q_ 20731750.h tml
http://www.experts-exchang
and
http://www.experts-exchang