Solved

Checkbox to update dropdown list with coldfusion query

Posted on 2004-10-14
7
849 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
  • 3
  • 3
7 Comments
 
LVL 35

Expert Comment

by:mrichmon
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 7

Accepted Solution

by:
black0ps earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility

<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
Comment Utility
Once again - thanks for your help
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In this short web based tutorial, I wanted to show users how they can still use the powers of FrontPage in conjunction with Expression Web 3.  Even though Microsoft eliminated the use of Web components, we can still use them with FrontPage and edit …
Turn A Profile Picture Into A Cartoon Using Photoshop And Illustrator This tutorial will teach you how to make a cartoon style image out of a regular picture. I have tried to keep the tutorial as simple as possible. I used Adobe CS4 for this tuto…
The purpose of this video is to demonstrate how to automatically show related posts at the bottom of a blog post in WordPress. This will be demonstrated using a Windows 8 PC. Plugin “Yet Another Related Posts Plugin” will be used. Go to your…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now