Dynamic dependant drop down

Greetings.

Found this code on http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_20663622.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

zerogAsked:
Who is Participating?
 
emphamyConnect With a Mentor Commented:
not sure why you are populating the arrays like you are you only have 2 fields yet you have an array with 3 sub keys?
0
 
zerogAuthor Commented:
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
0
 
emphamyConnect With a Mentor Commented:
check out some of these links, some you will have to put in your own php code, but its fairly easy

http://www.felgall.com/jstip22a.htm
http://codingforums.com/archive/index.php?t-48513.html
0
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

 
zerogAuthor Commented:
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 ...
0
 
emphamyConnect With a Mentor Commented:
all you have to do is replace the array's in js with php data so that is where you run your while loop and print out an array that reads like the code and that should do it.
0
 
zerogAuthor Commented:
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;
}
0
 
zerogAuthor Commented:
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

0
All Courses

From novice to tech pro — start learning today.