Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Checkbox to update dropdown list with coldfusion query

Posted on 2004-10-14
7
Medium Priority
?
868 Views
Last Modified: 2013-12-20
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
Comment
Question by:andystone2004
[X]
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
  • 3
  • 3
7 Comments
 
LVL 35

Expert Comment

by:mrichmon
ID: 12311076
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
 
LVL 7

Expert Comment

by:black0ps
ID: 12311686
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
 

Author Comment

by:andystone2004
ID: 12331716
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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
LVL 7

Accepted Solution

by:
black0ps earned 1000 total points
ID: 12333232
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
 

Author Comment

by:andystone2004
ID: 12337474
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
 
LVL 7

Expert Comment

by:black0ps
ID: 12338816

<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
 

Author Comment

by:andystone2004
ID: 12338890
Once again - thanks for your help
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Introduction In this tutorial, I'll explain how to create an animated progress meter in a wireframe prototype developed using Axure RP 7.0 - a leading prototyping tool for designing web sites and software. (For more information about Axure and gett…
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
The purpose of this video is to demonstrate how to Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…
The purpose of this video is to demonstrate how to integrate Mailchimp with Facebook. This will be demonstrated using a Windows 8 PC. Mailchimp and Facebook will be used. Log into your Mailchimp account. : Click on your name. Go to Account Setti…
Suggested Courses

704 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