Page 1 of 1

update a drop down menu from another one

Posted: Sat May 16, 2009 7:41 am
by wildrose
Hi

I made a page that contain two drop dowwn menus
the first for the name of the groups
The second for the name of the friends

when i choose from the first menu, the name of the friends that is related to that group must appear on the second one.

I wrote the following code:

Code: Select all

 
<?php
$conn=mysql_connect("localhost","root","");
mysql_select_db("exp",$conn);
 
 
 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
 
<script type="text/javascript" language="javascript">
 
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
 
 
</script>
<title>Untitled Document</title>
</head>
 
<body>
<form name="dpmenu" action="drop.php" method="post">
Select a Group:
<select name="groups"  id="groups" onchange="MM_jumpMenu('parent',this,0)">
 <?php 
 
 $sql="SELECT gpid,gpname FROM groups ";
$result = mysql_query("$sql",$conn);
 
while ($groups= mysql_fetch_array($result)) {
$gpid = $groups['gpid'];
$gpname = $groups['gpname'];
echo "<option value='drop.php?id=$gpid'>$gpname  </option>\n";
 }
 ?>
</select>
<p>
Select a Friend:
<select name="friends"  id="friends" >
<?php
 
 
$query="Select frid,fname,friends.gpid,groups.gpid from friends,groups where groups.gpid = friends.gpid ";
$res= mysql_query("$query",$conn);
 
while($friends= mysql_fetch_array($res))
{
 
//$fid = $friends['frid'];
$fname = $friends['fname'];
echo "
 <option value='$gpid' >$fname</option> \n";
 }
 
 
?>
</select>
</p>
</form>
</body>
</html>
 
I don't know what seems to be the problem here?
Can any one help me please?

Re: update a drop down menu from another one

Posted: Sat May 16, 2009 10:51 pm
by wildrose
please tell me how to solve that?

Re: update a drop down menu from another one

Posted: Sun May 17, 2009 1:55 am
by Benjamin
You must learn asynchronous JavaScript and XML. This will help you with your doubt.

Re: update a drop down menu from another one

Posted: Sun May 17, 2009 6:19 am
by wildrose
ok i know a littlebit about them but i need to accomplish this task as soon as possible
Is there an answer for my problem ?

Re: update a drop down menu from another one

Posted: Sun May 17, 2009 6:20 am
by DevGiven
Hi,

You should use ajax to populate the second drop down. You should assign to the first drop down a function to OnChange event that will grab the results.

Here is a complete tutorial and a demo on my blog how to build cascading drop down in php using mysql