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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

emphamyCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
emphamyCommented:
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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
emphamyCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.