zerog
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
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>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi. Thanks for your responses. I've been trying. Do you mean replace this with php code?
while($row1=mysql_fetch_ar ray($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.select edIndex].v alue;
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;
}
while($row1=mysql_fetch_ar
echo "arr1[".$p."]=new Array();";
echo "arr1[".$p."][0]='".$row1[
echo "arr1[".$p."][1]='".$row1[
echo "arr1[".$p."][2]='".$row1[
$p = $p+1;
}?>
function func(f){
var win = window.document.f1;
var k=0;
n = f.options[f.options.select
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][
k=k+1;
}
}
common.length=k;
}
ASKER
Greetings.
Thanks for the leads and the tip about the sub keys in the array. This is the working code
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>
ASKER