Connecting Tech Pros Worldwide Help | Site Map

CF Chained Selects

Member
 
Join Date: Mar 2007
Posts: 94
#1: Aug 24 '09
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.

Expand|Select|Wrap|Line Numbers
  1. <!--- store the selected Main_Group variable variable after the first select boxes submits itself --->
  2. <cfif isDefined('form.select_Main_Group')>
  3. <cfset page.select_Main_Group = form.select_Main_Group>
  4. </cfif> 
  5. <cfoutput>
  6. <form name="DropDown" method="post">
  7. <!--- query DB for the first drop down list --->
  8. <cfquery name="get_Main_Group" datasource="ds">
  9. SELECT group_id, group_name
  10. FROM tblGroups
  11. </cfquery>
  12. <!--- first drop down list --->
  13. <!--- NOTICE the onChange javascript event in the select tag, this is what submits the form after the first selection --->
  14. <select name="select_Main_Group" required="yes" onchange="this.form.submit()">
  15. <option>Select Main Group</option>
  16. <!--- dynamically populate the first drop down list based on the get_Main_Group query --->
  17. <!--- NOTICE the CFIF within the option tag, this says, if the first selection has been made, display the chosen option when the page reloads --->
  18. <cfloop query="get_Main_Group">
  19. <option value="#group_id#" <cfif isDefined('form.select_Main_Group')><cfif form.select_Main_Group eq "#group_id#">selected</cfif></cfif>>#group_name#</option>
  20. </cfloop>
  21. </select>
  22. <p>
  23. <!--- if the first selection has been made, display the second drop down list with the appropriate results --->
  24. <cfif isDefined('page.select_Main_Group')>
  25. <!--- query DB for second drop down list, based on the selected item from the first list --->
  26. <cfquery name="get_Sub_Group" datasource="ds">
  27. SELECT group_id, subgroup_id, subgroup_name
  28. FROM tblGroups 
  29. WHERE group_id = #page.select_Main_Group# 
  30. </cfquery>
  31. <!--- second drop down list --->
  32. <select name="select_Sub_Group" required="yes">
  33. <option>Select Subgroup</option>
  34. <!--- dynamically populate the second drop down list based on the get_Sub_Group query --->
  35. <cfloop query="get_Sub_Group">
  36. <option value="#subgroup_id#">#subgroup_name#</option>
  37. </cfloop>
  38. </select>
  39. </cfif>
  40. </form>
  41. </cfoutput>
  42.  
IF anyone has any ideas that might fix it, or any suggestions, it would be great!

Thanks
Neil
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: Sep 4 '09

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
#3: Sep 7 '09

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.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#4: Sep 7 '09

re: CF Chained Selects


Can you post the code for it?
Member
 
Join Date: Mar 2007
Posts: 94
#5: Sep 7 '09

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...
Expand|Select|Wrap|Line Numbers
  1. <cfif isDefined('form.SchemeType')>
  2.             <cfset page.SchemeType = form.SchemeType>
  3.             </cfif>
  4.  
  5.  
  6.                 <cfoutput>
  7.                 <form action="action_addrecord.cfm" method="post" name="form" onsubmit="return formCheck(this);">
  8.  
  9.                 <form name="" method="post">
  10.                 <cfquery name="get_Main_Group" datasource="wflow">
  11.                 select        SchemeType
  12.                 from        tblSchemeType
  13.                 </cfquery>        
  14.  
  15.                 <tr>
  16.                 <td class="highlight" style="width: 160px;">Document Type:</td>
  17.                 <td><select name="SchemeType" required="yes" onchange="this.form.submit()" class="droplist">
  18.                 <option>Select Scheme Type</option>
  19.                 <cfloop query="get_Main_Group">
  20.                 <option value="#SchemeType#" <cfif isDefined('form.SchemeType')><cfif form.SchemeType EQ "#SchemeType#">selected</cfif></cfif>>
  21.                 #SchemeType#</option>
  22.                 </cfloop>
  23.                 </select>
  24.                 </td>
  25.                 </tr>
  26.  
  27.  
  28.                 <cfif isDefined('page.SchemeType')>
  29.                 <cfquery name="get_Sub_Group" datasource="wflow">
  30.                 select Document, Type, OrderList
  31.                 from tblDocType
  32.                 where Type = '#page.SchemeType#'
  33.                 order by OrderList
  34.                 </cfquery>
  35.                 </form>
  36.  
  37.  
  38.  
  39.                 <tr>
  40.                 <td class="highlight" style="width: 160px;">Document Type:</td>
  41.                 <td><select name="DocumentType" required="yes" class="droplist">
  42.                 <option>Select Document Type</option>
  43.                 <cfloop query="get_Sub_Group">
  44.                 <option value="#Document#">#Document#</option>
  45.                 </cfloop>
  46.                 </select>
  47.                 </cfif>
  48.                 </td>
  49.                 </tr>
  50.  
  51.  
  52.             </cfoutput>
  53.  
  54.  
  55.  
  56.             <input type="hidden" name="Digitiser" value="#LoginName#" />
  57.  
  58.  
  59.             <tr>
  60.             <td class="highlight" style="width: 160px;">Scheme / Project Code:</td>
  61.             <td><input type="text" class="droplist" name="SchemeCode"  maxlength="40" size="40"></td>
  62.             </tr>
  63.  
  64.             <tr>
  65.             <td style="width: 180px;">Additional Scheme / Project Code:</td>
  66.             <td><input type="text" class="droplist" name="AddSchemeCode"  maxlength="40" size="40"></td>
  67.             </tr>
  68.  
  69.             <tr>
  70.             <td class="highlight">Scheme Title:</td>
  71.             <td colspan="3"><textarea class="droplist" name="SchemeTitle" cols="100" rows="3" wrap="virtual" class="droplist"></textarea></td>
  72.  
  73.             </tr>
  74.  
  75.  
  76.  
  77.             <tr>
  78.             <td class="highlight">Main Drawing No:</td>
  79.             <td><input type="text" class="droplist" name="DrawingNo" maxlength="40" size="40"></td>
  80.             <td style="width: 150px;">Developer:</td>
  81.             <td><input type="text" class="droplist" name="Developer" value=" " maxlength="30" size="26"></td>
  82.             </tr>
  83.  
  84.  
  85.  
  86.  
  87.  
  88.             <tr>
  89.             <td class="highlight">Received Date:</td>
  90.             <td><input type="text" name="RecDate" maxlength="10" size="12" class="droplist" onMouseover="ddrivetip('Enter the date using the calendar', 200)"; onMouseout="hideddrivetip()" >
  91.             <span style="padding-left: 5px;">
  92.             <a href="javascript:showCal('CalRecDate')" class="calendar"><img src="http://marlin/assetinl/WorkFlow/graphics/calendar.jpg" border="0" onMouseover="ddrivetip('Adds the date to the input field', 180)"; onMouseout="hideddrivetip()" /></a>
  93.             </td>
  94.             <td class="highlight">Scheme Date:</td>
  95.             <td><input type="text" name="SchemeDate" maxlength="10" size="12" class="droplist" onMouseover="ddrivetip('Enter the date using the calendar', 200)"; onMouseout="hideddrivetip()" >
  96.             <span style="padding-left: 5px;">
  97.             <a href="javascript:showCal('CalSchemeDate')" class="calendar"><img src="http://marlin/assetinl/WorkFlow/graphics/calendar.jpg" border="0" onMouseover="ddrivetip('Adds the date to the input field', 180)"; onMouseout="hideddrivetip()" /></a>
  98.             </td>
  99.             </tr>
  100.  
  101.             <tr>
  102.             <td colspan="4" style="height: 10px;">
  103.             </tr>
  104.  
  105.             <tr>
  106.             <td colspan="4">
  107.             <span class="headbanner">Sender Details</span>
  108.             </td>
  109.             </tr>
  110.  
  111.             <tr>
  112.             <td style="width: 150px;">Sent By:</td>
  113.             <td><input type="text" class="droplist" name="SentBy" value=" " maxlength="30" size="26"></td>
  114.             <td style="width: 150px;">Company:</td>
  115.             <td><select name="Company" size="1" class="droplist">
  116.             <cfoutput query="GetCompany">
  117.             <option value="#CompanyName#">#CompanyName#</option></cfoutput>
  118.             </td>
  119.             </tr>
  120.  
  121.             <tr>
  122.             <td style="width: 150px;">OS Mapsheet:</td>
  123.             <td><input type="text" class="droplist" name="OSRef" value=" " maxlength="12" size="26"></td>
  124.             <td style="width: 150px;">or Coords:</td>
  125.             <td style="width: 150px;">X: <input type="text" class="droplist" name="XRef" value=" " maxlength="10" size="6">
  126.             Y: <input type="text" class="droplist" name="YRef" value=" " maxlength="10" size="6"></td>
  127.             </tr>
  128.  
  129.  
  130.             <tr>
  131.             <td colspan="4" style="height: 10px;">
  132.             </tr>
  133.  
  134.             <tr>
  135.             <td colspan="4">
  136.             <span class="headbanner">Other Information</span>
  137.             </td>
  138.             </tr>
  139.  
  140.             <tr>
  141.             <td style="width: 150px;">Current Location:</td>
  142.             <td colspan="3"><input type="text" name="PhysLoc" class="droplist"  maxlength="40" size="40"></td>
  143.             </tr>
  144.  
  145.             <tr>
  146.             <td style="width: 150px;">Notes:</td>
  147.             <td colspan="3"><textarea name="RecordNotes" cols="100" rows="3" wrap="virtual" class="droplist" onMouseover="ddrivetip('Enter the scheme details here - what does it contain, or show?', 200)"; onMouseout="hideddrivetip()"></textarea></td>
  148.             </tr>
  149.  
  150.             </table>
  151.  
  152.             <input type="hidden" name="DateRecordAdded" value="<cfoutput>#dateFormat(Now(), 'dd/mm/yyyy')#</cfoutput>" />
  153.             <p />
  154.             <input type="submit" class="buttons" value="Submit Record" />
  155.             <input type="Reset" class="buttonswhite" value="Reset Form Fields" />
  156.  
  157.             </form>
  158.             </div>
  159.  
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#6: Sep 8 '09

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
#7: Sep 9 '09

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
#8: Sep 9 '09

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

Expand|Select|Wrap|Line Numbers
  1. <!--- store the selected Main_Group variable variable after the first select boxes submits itself --->
  2. <cfif isDefined('form.select_Main_Group')>
  3.     <cfset page.select_Main_Group = form.select_Main_Group>
  4. </cfif>
  5. <cfoutput>
  6.   <form name="DropDown" method="post">
  7.   <!--- query DB for the first drop down list --->
  8.   <cfquery name="get_Main_Group" datasource="ds">
  9.      SELECT group_id, group_name
  10.      FROM tblGroups
  11.   </cfquery>
  12.  
  13.  
  14.   <!--- first drop down list --->
  15.   <!--- NOTICE the onChange javascript event in the select tag, this is what submits the form after the first selection --->
  16.   <select name="select_Main_Group" required="yes" onchange="this.form.submit()">
  17.      <option>Select Main Group</option>
  18.      <!--- dynamically populate the first drop down list based on the get_Main_Group query --->
  19.      <!--- NOTICE the CFIF within the option tag, this says, if the first selection has been made, display the chosen option when the page reloads --->
  20.      <cfloop query="get_Main_Group">
  21.          <option value="#group_id#" <cfif isDefined('form.select_Main_Group')><cfif form.select_Main_Group eq "#group_id#">selected</cfif></cfif>>#group_name#</option>
  22.      </cfloop>
  23. </select>
  24. <p>
  25. <!--- if the first selection has been made, display the second drop down list with the appropriate results --->
  26. <cfif isDefined('page.select_Main_Group')>
  27.    <!--- query DB for second drop down list, based on the selected item from the first list --->
  28.    <cfquery name="get_Sub_Group" datasource="ds">
  29.         SELECT group_id, subgroup_id, subgroup_name
  30.         FROM tblGroups 
  31.         WHERE group_id = #page.select_Main_Group# 
  32.    </cfquery>
  33.  
  34.  
  35.    <!--- second drop down list --->
  36.    <select name="select_Sub_Group" required="yes">
  37.       <option>Select Subgroup</option>
  38.       <!--- dynamically populate the second drop down list based on the get_Sub_Group query --->
  39.       <cfloop query="get_Sub_Group">
  40.          <option value="#subgroup_id#">#subgroup_name#</option>
  41.       </cfloop>
  42.    </select>
  43. </cfif>
  44. </form>
  45. </cfoutput>
  46.  
  47. <!--- THAT'S ALL THERE IS TO IT! --->
  48.  
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#9: Sep 10 '09

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
#10: Sep 10 '09

re: CF Chained Selects


Cunning...

Thanks acoder, i'll give it a try and let you know how I got on.

Cheers
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#11: Sep 14 '09

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.
Reply


Similar ColdFusion bytes