Link to home
Start Free TrialLog in
Avatar of zerog
zerogFlag for South Africa

asked on

Dynamic dependant drop down

Greetings.

Found this code on https://www.experts-exchange.com/questions/20663622/Dynamic-dependent-dropdown-menus.html?sfQueryTermInfo=1+cneelu+dropdown+dynam

Have change the database selects for my database, but the second dropdown does not populate. My Javascript knowledge is limited. Seems all the stuff is in an array. What am I doing wrong, or what should I change for the second dropdown to populate based on the firsts selection.

Thanks

<?
      $connectid = mysql_connect($localhost,'root','');
      $dbase = recording;
      $res1 = mysql_db_query($dbase,"SELECT id,term FROM term");
      $res2 = mysql_db_query($dbase,"SELECT id,task_title FROM tasks");
?>
<html>
<head>
<script language="javascript">
var arr1=new Array();
<?
$p = 0;
while($row1=mysql_fetch_array($res2)){
      echo "arr1[".$p."]=new Array();";
      echo "arr1[".$p."][0]='".$row1[0]."';";
      echo "arr1[".$p."][1]='".$row1[1]."';";
      echo "arr1[".$p."][2]='".$row1[2]."';";
      $p = $p+1;
}?>
function func(f){
            var win = window.document.f1;
            var k=0;
            n = f.options[f.options.selectedIndex].value;
            common = win.s2;
            common.length=arr1.length
            for(i=0;i<arr1.length;i++){
                  if(n==arr1[i][2]){                  
                  common.options[k] = new Option(arr1[i][1],arr1[i][0]);
                  k=k+1;
                  }
            }
            common.length=k;
}
</script>
</head>
<body onload="func(window.document.f1.s1)">
<form name="f1">
<select onchange="func(this);" name="s1">
<?while($row=mysql_fetch_array($res1)){?>
<option value="<?echo $row['id'];?>"><?echo $row['term'];?></option>
<?}?>
</select><br>
<select name="s2">
</option>
</select>
</form>
</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of emphamy
emphamy

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
Avatar of zerog

ASKER

Thanks for response. I got this code from a previous post and I'm not too sure what to do or modify to get it working, but will look again at the sub keys
SOLUTION
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
Avatar of zerog

ASKER

Thanks for the response. Had a look at the links, but I'm panicking my javascript knowledge limited. Will I put those stuff where the javascripts bits in my code are or with the php array stuff ...
SOLUTION
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
Avatar of zerog

ASKER

Hi. Thanks for your responses. I've been trying. Do you mean replace this with php code?

while($row1=mysql_fetch_array($res2)){
      echo "arr1[".$p."]=new Array();";
      echo "arr1[".$p."][0]='".$row1[0]."';";
      echo "arr1[".$p."][1]='".$row1[1]."';";
      echo "arr1[".$p."][2]='".$row1[2]."';";
      $p = $p+1;
}?>
function func(f){
            var win = window.document.f1;
            var k=0;
            n = f.options[f.options.selectedIndex].value;
            common = win.s2;
            common.length=arr1.length
            for(i=0;i<arr1.length;i++){
                  if(n==arr1[i][2]){                  
                  common.options[k] = new Option(arr1[i][1],arr1[i][0]);
                  k=k+1;
                  }
            }
            common.length=k;
}
Avatar of zerog

ASKER

Greetings.

Thanks for the leads and the tip about the sub keys in the array. This is the working code
<?
      $connectid = mysql_connect($localhost,'root','');
      $dbase = recording;
      $res1 = mysql_db_query($dbase,"SELECT id,term FROM term");
      $res2 = mysql_db_query($dbase,"SELECT id,task_title,term FROM tasks");
?>
<html>
<head>
<script language="javascript">
var arr1=new Array();
<?
$p = 0;
while($row1=mysql_fetch_array($res2)){
      echo "arr1[".$p."]=new Array();";
      echo "arr1[".$p."][0]='".$row1[0]."';";
      echo "arr1[".$p."][1]='".$row1[1]."';";
      echo "arr1[".$p."][2]='".$row1[2]."';";
      $p = $p+1;
}?>
function func(f){
            var win = window.document.f1;
            var k=0;
            n = f.options[f.options.selectedIndex].value;
            common = win.s2;
            common.length=arr1.length
            for(i=0;i<arr1.length;i++){
                  if(n==arr1[i][2]){                  
                  common.options[k] = new Option(arr1[i][1],arr1[i][0]);
                  k=k+1;
                  }
            }
            common.length=k;
}
</script>
</head>
<body onload="func(window.document.f1.s1)">
<form method="post" name="f1" action="process.php">
<select onchange="func(this);" name="s1">
<option value="">Select Term</option>
<?while($row=mysql_fetch_array($res1)){?>
<option value="<?echo $row['id'];?>"><?echo $row['term'];?></option>
<?}?>
</select><br>
<select name="s2">
</option>
</select>
<input type="submit" value="Submit">
</form>
</body>
</html>

Open in new window