I came across the same problem a few months back at work and, like you, not the world's greatest at JavaScript. But I did manage to find a solution, so here goes.
The company I work for deals with the maintenance of aircraft, and needed to be able to select an airline from the first drop-down box and then select an aircraft registration from the second drop-down box. e.g.
<form name="frmAircraft">
<select name="AirlineID" onChange="aircraft_details()" size=1>
I populated the first drop-down box with the airline's name and their relevant codes e.g.
<option value="<?php echo $row["AirlineID"]; ?>"><?php echo $row["AirlineName"]; ?></option>....</select>
Now comes the first JavaScript bit. I had to create and array for the airline codes, and another array for the aircraft relating to the airline.
<SCRIPT LANGUAGE="JavaScript">
<?php
$qryA = "SELECT AirlineID from tblAirline";
$resA = mysql_query($qryA, $db);
for ($i=0; $i < mysql_num_rows($resA); $i++) {
$rowA = mysql_fetch_array($resA);
printf("var %s = new Array()\n", $rowA["AirlineID"]);
printf("var %s2 = new Array()\n", $rowA["AirlineID"]);
$qryB = "SELECT ACReg, ACType FROM tblAircraft WHERE AirlineID='" . $rowA["AirlineID"] . "' ORDER BY ACReg";
$resB = mysql_query($qryB, $db);
for ($j=0; $j < mysql_num_rows($res๐; $j++) {
$rowB = mysql_fetch_array($res๐;
printf("%s[%s] = \"%s\"\n", $rowA["AirlineID"], $rowB["ACReg"]);
printf("%s2[%s] = \"%s, %s\"\n", rowA["AirlineID"], $rowB["ACReg"], $rowB["ACType"]);
}
}
?>
</SCRIPT>
I then created the second drop-down box for the aircraft registrations and the aircraft types e.g.
<select name="ACReg" size=4><option value="none">Select Airline</option></select>
The second JavaScript defines the aircraft_details() function, which does the donkey work!
<SCRIPT LANGUAGE="JavaScript">
function aircraft_details() {
if (document.frmAircraft.AirlineID.options.selectedIndex == -1) {
} else {
while (document.frmAircraft.ACReg.options.length) {
document.frmAircraft.ACReg.options[0] = null;
}
count = 0;
airlineList = document.frmAircraft.ACReg.options;
for (var n in eval(document.frmAircraft.AirlineID.options[document.frmAircraft.AirlineID.selectedIndex].value)) {
airlineList[count] = new Option(eval(document.frmAircraft.AirlineID.options[document.frmAircraft.AirlineID.selectedIndex].value + '[' + [count] + ']'));
airlineList[count].value = eval(document.frmAircraft.options[document.frmAircraft.AirlineID.selectedIndex].value + '[' + [count] + ']');
airlineList[count].text = eval(document.frmAircraft.options[document.frmAircraft.AirlineID.selectedIndex].value + '2[' + [count] + ']');
count++;
}
}
}
</SCRIPT>
The first part of the if statement checks to see if an airline has actually been selected (-1 means that it hasn't).
The while statement empties the aircraft drop-down box, because if your first selection has more items than your second, then these will still appear at the bottom of your drop-down box.
The for statement builds the new items to 'airlineList', which are the options for the ACReg drop-down box.
And that is it .... I think! If you want to have a look at a working example, visit the easyJet website and have a look when you select an airport - the list of destinations reduces automatically.
Hope this helps to solving your problem!