Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Checkbox to update dropdown list with coldfusion query

Posted on 2004-10-14
7
Medium Priority
?
872 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
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
Lease-to-own eliminates the expenditure of hardware replacement and allows you to pay off the server over time. Usually, this is much cheaper than leasing servers. Think of lease-to-own as credit without interest.
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…

926 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