update a drop down menu from another one

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wildrose
Forum Newbie
Posts: 5
Joined: Fri Apr 24, 2009 7:17 am

update a drop down menu from another one

Post 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?
Last edited by Benjamin on Sat May 16, 2009 10:18 am, edited 1 time in total.
Reason: Added [code=php] tags.
wildrose
Forum Newbie
Posts: 5
Joined: Fri Apr 24, 2009 7:17 am

Re: update a drop down menu from another one

Post by wildrose »

please tell me how to solve that?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: update a drop down menu from another one

Post by Benjamin »

You must learn asynchronous JavaScript and XML. This will help you with your doubt.
wildrose
Forum Newbie
Posts: 5
Joined: Fri Apr 24, 2009 7:17 am

Re: update a drop down menu from another one

Post 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 ?
DevGiven
Forum Newbie
Posts: 7
Joined: Wed Mar 04, 2009 5:41 pm

Re: update a drop down menu from another one

Post 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
Last edited by Benjamin on Sun May 17, 2009 9:31 am, edited 1 time in total.
Reason: Removed quote of entire post.
Post Reply