• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 878
  • Last Modified:

Checkbox to update dropdown list with coldfusion query

Hi,

What I'd like to do is add to/subtract from a dropdown list with a coldfusion query using checkboxes.

e.g - say I have 4 checkboxes whose values are car makes - ford, audi, bmw, vw. when none of them are checked the dropdown says the usual 'select' and nothing else, but when say VW is checked it queries my database (Access) gets the models associated with VW - Golf, Beetle etc etc and adds them to my dropdown list and when the checkbox is unchecked it removes them.

When more than one checkboxes are checked it just adds them and gives them a heading in the dropdown list as follows: (Audi and VW have been checked)

Select
=========
**Audi**
TT
A3
A4
etc etc
**Volkswagon**
Golf
Beetle

Any help would be much appreciated,

Thanks
0
andystone2004
Asked:
andystone2004
  • 3
  • 3
1 Solution
 
mrichmonCommented:
The easiest way I can think of is to have it so that when a box is checked it works like this:

Remove all selections from drop down.
Add selections for each checked box.

That way you don't have to worry about duplicate model names or about storing additional info in the value.

I would do something like 2 related drop downs, but have the function happen onclick of the checkboxes instead of onchange of the first drop-down.

Here is code for two related drop downs....

<script type="text/javascript" language="JavaScript">
<!--
/* Dynamically populate Buyer select box based on seleted DepartmentID */
// Create an array to hold Buyers
var BuyerArray = new Array;

// Define a custom Javascript object type to represent a single Buyer
function Buyer(BuyerUsername, DepartmentID, BuyerName)
{
     this.BuyerUsername = BuyerUsername;
     this.DepartmentID = DepartmentID;
     this.BuyerName = BuyerName;
}
<!--- For each Buyer, append a new Buyer object to the array of Buyers --->
<cfoutput query="GetBuyers">
     BuyerArray[BuyerArray.length] = new Buyer("#JSStringFormat(Username)#", "#JSStringFormat(DepartmentID)#", "#JSStringFormat(BuyerName)#");
</cfoutput>

/* Fill the Buyer select box based on the DepartmentID */
function fillBuyers()
{
     // Stop if there is no selected ParentID
     if (document.AddIssue.DepartmentID.selectedIndex == -1)
     {
          return;
     }
     
     var DepartmentID = document.AddIssue.DepartmentID.options[document.AddIssue.DepartmentID.selectedIndex].value;
     
     // Remove all options in the Buyer select box
     document.AddIssue.Buyer.options.length = 0;
     
     // For each item in the Buyer array ...
     for (var i = 0; i < BuyerArray.length; i++)
     {
          // If the Buyer's DepartmentID is the same as the currently selected DepartmentID
          if (BuyerArray[i].DepartmentID == DepartmentID)
          {
               // Put a new option in the Buyer select box
               document.AddIssue.Buyer.options[document.AddIssue.Buyer.options.length] = new Option(BuyerArray[i].BuyerName, BuyerArray[i].BuyerUsername);
          }
     }
}
-->
</script>


<select name="DepartmentID" onChange="fillBuyers()" style="width: 200px;">
     <option value=""></option>
     <option value="GM">General Merchandise</option>
     <option value="TR">Trade</option>
     <option value="TX">Text</option>
</select>

<select name="Buyer" style="width: 200px;"></select>


You still need to have the javascript objects built, and then just modify the filldropdown to work off of the checkboxes instead of a first drop-down...
0
 
black0psCommented:
I tested this and it works well (looks like it might be less code than above too). Our company does seminars and so I've used seminars and seminar types as an example. Seminar types would be like car makes and seminars would be models. Let me know if you have questions about my code below:

<!--- Select only the fields that are necessary to populate drop down --->
<cfquery name="Seminars" datasource="#Request.DS#" username="#Request.UN#" password="#Request.PW#">
Select s.DateStart,s.SeminarID,st.Type,st.SeminarTypeID From Seminars s Inner Join SeminarTypes st On s.SeminarTypeID = st.SeminarTypeID
Order By st.SeminarTypeID
</cfquery>
<cfquery name="SeminarTypes" dbtype="query">
Select SeminarTypeID, Type From Seminars
Group By SeminarTypeID,Type
</cfquery>

<!--- Load the wddx.js file, which includes the dump function --->
<script type="text/javascript" src="/CFIDE/scripts/wddx.js"></script>
<script language="JavaScript">
<!--
// Output cf query into javascript array
<cfwddx action="cfml2js" input="#Seminars#" topLevelVariable="sem">
// Output javascript array in a table
//document.write(sem.dump(true));
// Set number of text boxes in JS Array
<cfwddx action="cfml2js" input="#SeminarTypes#" topLevelVariable="sembox">
//document.write(sembox.dump(true));

function addOptions() {
      document.Seminars.SeminarID.options.length = 1;
      for (i=0;i<sembox["type"].length;i++) {
            if (document.Seminars["chk"+sembox["seminartypeid"][i]].checked == true) {
                  for (j=0;j<sem["type"].length;j++) {
                        if (sem["seminartypeid"][j] == sembox["seminartypeid"][i]) {
                              document.Seminars.SeminarID.options[document.Seminars.SeminarID.options.length] = new Option(sem["type"][j] + " " + sem["datestart"][j],sem["seminarid"][j]);
                        }
                  }
            }
      }
}
//-->
</script>


<form name="Seminars">
<cfoutput query="Seminars" group="Type">
      <input name="chk#Seminars.SeminarTypeID#" type="checkbox" onclick="addOptions()" value="#Seminars.SeminarTypeID#"> #Seminars.Type#<br /></cfoutput>
      <select name="SeminarID">
            <option value="">-- Choose Seminar --</option>
            <option value="t">test</option>
      </select>
</form>
0
 
andystone2004Author Commented:
This is the kind of thing I had in mind, but in my case I want to hard code the check boxes so that they are not created dynamically and are not dependent on the DB.
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
black0psCommented:
Gotcha. Hard code them into the script but stick with the same CF Code. It will be a easier to have it dependent on the DB for future changes, but it's up to you.

With the code below, add the check boxes to the document, then make sure to add their names into the chkList array.

<!--- Select only the fields that are necessary to populate drop down --->
<cfquery name="Seminars" datasource="#Request.DS#" username="#Request.UN#" password="#Request.PW#">
Select DateStart,SeminarID,SeminarTypeID From Seminars
</cfquery>
<!--- Load the wddx.js file, which includes the dump function --->
<script type="text/javascript" src="/CFIDE/scripts/wddx.js"></script>
<script language="JavaScript">
<!--
// Output cf query into javascript array
<cfwddx action="cfml2js" input="#Seminars#" topLevelVariable="sem">

// List of checkboxes in array
chkList = new Array("chk1","chk2","chk3");

function addOptions() { // Added x1 for values
     document.Seminars.SeminarID.options.length = 1; // Remove all options
     for (i=0;i<chkList.length;i++) { // Loop through check boxes
                              if (document.Seminars[chkList[i]].checked == true) { // If checkbox is checked
               for (j=0;j<sem["seminarid"].length;j++) { // Loop through seminars in query
                    if (sem["seminartypeid"][j] == document.Seminars[chkList[i]].value) { // If values are the same add option
                         document.Seminars.SeminarID.options[document.Seminars.SeminarID.options.length] = new Option(sem["datestart"][j],sem["seminarid"][j]);
                    }
               }
          }
     }
}
//-->
</script>


<form name="Seminars">
      <!--- Add your own values and make sure the correspond with the query --->
      <input name="chk1" type="checkbox" onClick="addOptions()" value="1">MyType<br />
      <input name="chk2" type="checkbox" onClick="addOptions()" value="2">MyType<br />
      <input name="chk3" type="checkbox" onClick="addOptions()" value="3">MyType<br />
      <select name="SeminarID">
            <option value="">-- Choose Seminar --</option>
            <option value="t">test</option>
      </select>
</form>
0
 
andystone2004Author Commented:
Works perfectly, Thankyou very much.

Is It possible for you to help me on one more thing though, I have a query and want to loop over it and group by car makes:

<cfquery name="CarList" datasource="#DB#">SELECT * FROM Cars WHERE CarMake = '****' ORDER BY LastName Asc</cfquery>

where **** is the car make I want to group by.

So I could have 20 Audi entry's, 10 BMW entry's but just need to loop through them once for each car make instead of doing this:

<cfquery name="CarList1" datasource="#DB#">SELECT * FROM Cars WHERE CarMake = 'Audi' </cfquery>

<cfquery name="CarList2" datasource="#DB#">SELECT * FROM Cars WHERE CarMake = 'BMW' </cfquery>

So the loop output will output all the car makes I have alphebetically:

Audi
BMW
etc
etc
0
 
black0psCommented:

<cfquery name="CarList" datasource="#DB#">
SELECT * FROM Cars ORDER BY LastName Asc
</cfquery>

<cfoutput query="CarList" group="CarMake">
  <h4>#CarList.CarMake#</h4>
  <cfoutput>
    <p>#CarList.CarModel#</p>
  </cfoutput>
</cfoutput>
0
 
andystone2004Author Commented:
Once again - thanks for your help
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now