Link to home
Start Free TrialLog in
Avatar of concep86
concep86

asked on

Dropdown menus

I have a form that I am creating that will consist of 2 dropdowns/combo boxes.  What I want to know is how do I create a combo box that changes based on the selection made in the first combo box.  Both combo boxes will be populated from an sql database.

example...  combo1 has the name of the manufacturers and combo2 the names of the products.  When I select the name of the manufacturer I want the second box to only display that manufacturers products...
I know how to do it by hardcoding the dropdowns but I want it to come from the database....

Avatar of sybe
sybe



<FORM name="myForm">
<SELECT Name="Box1" onChange="ChangeBox2(this.options[this.selectedIndex].value)">
<option> ---------------------------------------
<option>
<option>
<option>
<option>
<option>
<option>
<option>
<option>
</SELECT>
<p>

<SELECT Name="Box2">
<option> ---------------------------------------
<option>
<option>
<option>
<option>
<option>
<option>
<option>
</SELECT>

</form>

<script>
// ============================================================
// The visible size selectboxes is predefined with the HTML above
// The following deletes all entries, but the size remains intact.

i = 0
while (document.forms['myForm'].Box1.options.length) {
        document.forms['myForm'].Box1.options[i] = null;
}
i = 0
while (document.forms['myForm'].Box2.options.length) {
        document.forms['myForm'].Box2.options[i] = null;
}

// ============================================================
// The following code generates arrays which are used to fill the dropdown boxes
// Each 'option' is defined by arrName[series][option][text or value]
// All arrays with the same "series" go together into one box
// Each option has a text (which is displayes) and a value (which is sent on submit)
// The "series" of the second box, correspond with the value of the first box

arrBox1 = new Array()
arrBox2 = new Array()

arrBox1[0] = new Array()
arrBox1[0][0] = new Array();
arrBox1[0][0][0] ='subject1';
arrBox1[0][0][1] =1;

arrBox1[0][1] = new Array();
arrBox1[0][1][0] ='subject2';
arrBox1[0][1][1] =2;

arrBox1[0][2] = new Array();
arrBox1[0][2][0] ='subject3';
arrBox1[0][2][1] =3;



arrBox2[1] = new Array()
arrBox2[1][0] = new Array();
arrBox2[1][0][0] ='belongs to subject1 (a)';
arrBox2[1][0][1] =1;

arrBox2[1][1] = new Array();
arrBox2[1][1][0] ='belongs to subject1 (b)';
arrBox2[1][1][1] =2;

arrBox2[1][2] = new Array();
arrBox2[1][2][0] ='belongs to subject1 (c)';
arrBox2[1][2][1] =3;



arrBox2[2] = new Array()
arrBox2[2][0] = new Array();
arrBox2[2][0][0] ='belongs to subject2 (a)';
arrBox2[2][0][1] =1;

arrBox2[2][1] = new Array();
arrBox2[2][1][0] ='belongs to subject2 (b)';
arrBox2[2][1][1] =2;

arrBox2[2][2] = new Array();
arrBox2[2][2][0] ='belongs to subject2 (c)';
arrBox2[2][2][1] =3;



arrBox2[3] = new Array()
arrBox2[3][0] = new Array();
arrBox2[3][0][0] ='belongs to subject3 (a)';
arrBox2[3][0][1] =1;

arrBox2[3][1] = new Array();
arrBox2[3][1][0] ='belongs to subject3 (b)';
arrBox2[3][1][1] =2;

arrBox2[3][2] = new Array();
arrBox2[3][2][0] ='belongs to subject3 (c)';
arrBox2[3][2][1] =3;


// ============================================================
// functions:
// The functions implement the arrays into the dropdown boxes.
// First this fills Box1 for start (box 2 remains empty)

i = 0
j = 0
while (i < arrBox1.length) {
   while (j < arrBox1[i].length) {
      document.forms['myForm'].Box1.options[j] = new Option(arrBox1[i][j][0], arrBox1[i][j][1], true, true);
      j++;
   }
   i++;
}


// This is the function that is called onChange of Box1.
// the value of the selected item is passed to the function
// based on this value the "series" of the arrays for Box is selected

function ChangeBox2(ChoiceMade) {
   i = 0
   while (document.forms['myForm'].Box2.options.length) {
      document.forms['myForm'].Box2.options.options[i] = null;
   }
   i = 0
   while (i < arrBox2[ChoiceMade].length) {
      if (i == 0) {
         document.forms['myForm'].Box2.options[i] = new Option(arrBox2[ChoiceMade][i][0], arrBox2[ChoiceMade][i][1], true, true);
      } else {
         document.forms['myForm'].Box2.options[i] = new Option(arrBox2[ChoiceMade][i][0], arrBox2[ChoiceMade][i][1]);
      }
      i++;
  }
}


</script>
Avatar of concep86

ASKER

Adjusted points to 75
How will the data come from the database?  Where do I define the data for the fields...  Sorry, I just dont see it.

Thanks.  
Hey, you asked this in the javascript area, you get a javascript answer.

I have sample code getting this from a database using ASP, but it would probably quite unreadable if you don't know the code above.

Do you have a database ?? What does it look like ??

Do you have any serverside scripting ?? what language ??





I am not totally clueless on this code...  I see how it is working. Enough to be able to modify it anyway.  Thanks.  I do have several programmers I can get help from if you give me the ASP code you are making reference to.  

My database is sql,  The tables are ITEM and VENDORS.  I am populating the first combo from the VENDORS table and am looking at the second combo to fill in the items based on the vendor choice...  

All help is appreciated...

again if you could give me the sample ASP code you have, I am sure I'll be able to figure it out...

I am not totally clueless on this code...  I see how it is working. Enough to be able to modify it anyway.  Thanks.  I do have several programmers I can get help from if you give me the ASP code you are making reference to.  

My database is sql,  The tables are ITEM and VENDORS.  I am populating the first combo from the VENDORS table and am looking at the second combo to fill in the items based on the vendor choice...  

All help is appreciated...

again if you could give me the sample ASP code you have, I am sure I'll be able to figure it out...

I am not totally clueless on this code...  I see how it is working. Enough to be able to modify it anyway.  Thanks.  I do have several programmers I can get help from if you give me the ASP code you are making reference to.  

My database is sql,  The tables are ITEM and VENDORS.  I am populating the first combo from the VENDORS table and am looking at the second combo to fill in the items based on the vendor choice...  

All help is appreciated...

again if you could give me the sample ASP code you have, I am sure I'll be able to figure it out...

ASKER CERTIFIED SOLUTION
Avatar of sybe
sybe

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you.