CF Chained Selects | Member | | Join Date: Mar 2007
Posts: 94
| |
Hi there,
Does anyone know of a decent chained selects script in CF? I'd like one that can still accept query values from the database.
I found an excellent script on EasyCFM but because it uses a form I can't get it to work with my existing form on the page - clicking the dropdown executes the onSubmit action of my form and it errors out. - <!--- store the selected Main_Group variable variable after the first select boxes submits itself --->
-
<cfif isDefined('form.select_Main_Group')>
-
<cfset page.select_Main_Group = form.select_Main_Group>
-
</cfif>
-
<cfoutput>
-
<form name="DropDown" method="post">
-
<!--- query DB for the first drop down list --->
-
<cfquery name="get_Main_Group" datasource="ds">
-
SELECT group_id, group_name
-
FROM tblGroups
-
</cfquery>
-
<!--- first drop down list --->
-
<!--- NOTICE the onChange javascript event in the select tag, this is what submits the form after the first selection --->
-
<select name="select_Main_Group" required="yes" onchange="this.form.submit()">
-
<option>Select Main Group</option>
-
<!--- dynamically populate the first drop down list based on the get_Main_Group query --->
-
<!--- NOTICE the CFIF within the option tag, this says, if the first selection has been made, display the chosen option when the page reloads --->
-
<cfloop query="get_Main_Group">
-
<option value="#group_id#" <cfif isDefined('form.select_Main_Group')><cfif form.select_Main_Group eq "#group_id#">selected</cfif></cfif>>#group_name#</option>
-
</cfloop>
-
</select>
-
<p>
-
<!--- if the first selection has been made, display the second drop down list with the appropriate results --->
-
<cfif isDefined('page.select_Main_Group')>
-
<!--- query DB for second drop down list, based on the selected item from the first list --->
-
<cfquery name="get_Sub_Group" datasource="ds">
-
SELECT group_id, subgroup_id, subgroup_name
-
FROM tblGroups
-
WHERE group_id = #page.select_Main_Group#
-
</cfquery>
-
<!--- second drop down list --->
-
<select name="select_Sub_Group" required="yes">
-
<option>Select Subgroup</option>
-
<!--- dynamically populate the second drop down list based on the get_Sub_Group query --->
-
<cfloop query="get_Sub_Group">
-
<option value="#subgroup_id#">#subgroup_name#</option>
-
</cfloop>
-
</select>
-
</cfif>
-
</form>
-
</cfoutput>
-
IF anyone has any ideas that might fix it, or any suggestions, it would be great!
Thanks
Neil
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: CF Chained Selects
Replace the form with your form and elements. What does your form look like?
| | Member | | Join Date: Mar 2007
Posts: 94
| | | re: CF Chained Selects
My form is a simple record filling one but has some onSubmit javascript that runs some error checking stuff. I've tried replacing the elements but the form just changes the dropdown instead of returning the values to the database.
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: CF Chained Selects
Can you post the code for it?
| | Member | | Join Date: Mar 2007
Posts: 94
| | | re: CF Chained Selects
Sure - let me dig up some of the old code as I had replaced it with some chained select javascript code. Ideally I would like the CF version as it's still dynamic, whereas the javascript is hard coded.
Here it is... |  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: CF Chained Selects
I see you have a nested form (see line 9). You should only have one form.
| | Member | | Join Date: Mar 2007
Posts: 94
| | | re: CF Chained Selects
Yes...I found the script on EasyCfm and it just takes the values from the database table and passes them into the drop down. I have used the form as is and integrated it into the rest of my code but the onClick event runs the drop down - it doesn't post the record.
I`ll try and dig it out so you can see it.
| | Member | | Join Date: Mar 2007
Posts: 94
| | | re: CF Chained Selects
Here's the code. I got it to work fine on its own, but couldn't integrate it into my form, because the onChange event tried to post the record.
This is Tutorial 166 from Easy CFM - <!--- store the selected Main_Group variable variable after the first select boxes submits itself --->
-
<cfif isDefined('form.select_Main_Group')>
-
<cfset page.select_Main_Group = form.select_Main_Group>
-
</cfif>
-
<cfoutput>
-
<form name="DropDown" method="post">
-
<!--- query DB for the first drop down list --->
-
<cfquery name="get_Main_Group" datasource="ds">
-
SELECT group_id, group_name
-
FROM tblGroups
-
</cfquery>
-
-
-
<!--- first drop down list --->
-
<!--- NOTICE the onChange javascript event in the select tag, this is what submits the form after the first selection --->
-
<select name="select_Main_Group" required="yes" onchange="this.form.submit()">
-
<option>Select Main Group</option>
-
<!--- dynamically populate the first drop down list based on the get_Main_Group query --->
-
<!--- NOTICE the CFIF within the option tag, this says, if the first selection has been made, display the chosen option when the page reloads --->
-
<cfloop query="get_Main_Group">
-
<option value="#group_id#" <cfif isDefined('form.select_Main_Group')><cfif form.select_Main_Group eq "#group_id#">selected</cfif></cfif>>#group_name#</option>
-
</cfloop>
-
</select>
-
<p>
-
<!--- if the first selection has been made, display the second drop down list with the appropriate results --->
-
<cfif isDefined('page.select_Main_Group')>
-
<!--- query DB for second drop down list, based on the selected item from the first list --->
-
<cfquery name="get_Sub_Group" datasource="ds">
-
SELECT group_id, subgroup_id, subgroup_name
-
FROM tblGroups
-
WHERE group_id = #page.select_Main_Group#
-
</cfquery>
-
-
-
<!--- second drop down list --->
-
<select name="select_Sub_Group" required="yes">
-
<option>Select Subgroup</option>
-
<!--- dynamically populate the second drop down list based on the get_Sub_Group query --->
-
<cfloop query="get_Sub_Group">
-
<option value="#subgroup_id#">#subgroup_name#</option>
-
</cfloop>
-
</select>
-
</cfif>
-
</form>
-
</cfoutput>
-
-
<!--- THAT'S ALL THERE IS TO IT! --->
-
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: CF Chained Selects
One possible solution to this problem is to set a variable when the form is submitted via onchange. Then in your onsubmit validation code, check for this variable. If set, return true, so it doesn't validate before submitting.
| | Member | | Join Date: Mar 2007
Posts: 94
| | | re: CF Chained Selects
Cunning...
Thanks acoder, i'll give it a try and let you know how I got on.
Cheers
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: CF Chained Selects
If you want to get your hands dirty with JavaScript/Ajax, then there would be no need for a submit and hence no conflict, but I guess that depends on expertise and how much time you're willing to invest in this problem.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,223 network members.
|