Solved

Checkbox to update dropdown list with coldfusion query

Posted on 2004-10-14
7
861 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 7

Accepted Solution

by:
black0ps earned 250 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

When setting up new project requests for our site, one of the most powerful tools our team has available to use is Axure (http://www.axure.com/). It’s a tool for creating software and web prototypes that can function and interact as if it were the a…
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
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 manually back up a WordPress Database. This will be demonstrated using a Windows 8 PC. The Host used will be IPage.com Log into your Hosting account. IPage will be used for demonstration : Locat…

734 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