...

View Full Version : Dynamically populating one dropdown menu, from value selected in another dropdown



mhunt
06-23-2005, 04:19 AM
Hi, i have a database filled with things and the way i want the client to be able to edit it is by first selecting the product from one drop down menu, then from whatever they choose in that drop down menu, the second is populated with the information that goes with it. Heres what i have:


<?php
if($session->isAdmin()){
?>
<form action="" method="post" name="gun" class="forms" enctype="multipart/form-data">
Select a Manufacturer:<br />
<select name="manufacturer">
<option value=""></option>
<?php
$query = "SELECT * FROM `gun_manufacturers`";
$result = mysql_query($query, $conn);
$numrows = mysql_num_rows($result);
for ($i=0; $i < $numrows; $i++)
{
$row = mysql_fetch_array($result, MYSQL_ASSOC);
echo "<option value=\"{$row['gunManufacturerID']}\">{$row['gunManufacturerName']}</option>";
}
?>
</select><br />
<select name="gun">
<?php
$gunManufacturerID = ( get_magic_quotes_gpc() ) ? $_POST['manufacturer'] : addslashes( $_POST['manufacturer'] );
$query = "SELECT * FROM `guns` WHERE `gunManufacturerID = {$gunManufacturerID}";
$result = mysql_query($query, $conn);
$numrows = mysql_num_rows($result);
for ($i=0; $i < $numrows; $i++)
{
$row = mysql_fetch_array($result, MYSQL_ASSOC);
echo "<option value=\"{$row['gunID']}\">{$row['gunName']}</option>";
}

?>
</select>
</form>
<?
} else {
echo "Sorry you do not have access to this page.";
}
?>

I know there is probably an easier way to do those queries too with joins, but im not too experienced with them and I can't figure out what the join would look like. Any help is greatly appreaciated

mhunt
06-23-2005, 06:46 PM
anyone?

emehrkay
06-23-2005, 09:00 PM
i think you should use jumpmenus and their targets would be to refresh the same page but pass variables through so that the next jumpmenu and do a $_GET then a query based on that $_GET then build the menu

mhunt
06-23-2005, 10:44 PM
any idea what that code would look like?

emehrkay
06-24-2005, 02:44 PM
well this is how dreamweaver does jump menus


<?
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0;
}
//-->
</script>
</head>

<body>
<form name="form1" method="post" action="">
<select name="menu1" onChange="MM_jumpMenu('parent',this,0)">
<option value="test.php?Q=SELECT * FROM table BLAH" selected>test</option>
</select>
</form>
?>


now you could set the value = your page.php?then the query then for the next menu you could do

$query = $_GET['Q'];
$result = mysql_query($query) or die ("COULD NOT GET QUERY FROM URL: ".mysql_error());
echo "<select name =\"xxx\">"
while($row = mysql_fetch_array($result)){
then youll bulid the menu here
echo "<option value =\"xxx\">".$row['table column name']."</option>"
}
echo "</select>";

or you could just keep building jump menu's until you reach you desired limit

mhunt
06-24-2005, 06:34 PM
oh nice man let me see if this works, thanks.

mhunt
06-24-2005, 06:59 PM
this keeps returning, "Query was empty".


<script language="javascript" type="text/javascript">
<!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0;
}
//-->
</script>
<form action="" method="post" name="gun" class="forms" enctype="multipart/form-data">
Select a Manufacturer:<br />
<select name="manufacturer" onChange="MM_jumpMenu('parent',this,0)">
<option value="edit_guns.php?Q=SELECT * FROM guns LEFT JOIN gun_manufacturers ON guns.gunManufacturerID=gun_manufacturers.gunManufacturerID" selected>Select a Manufacturer</option>
</select>
</form>
<?
$query = $_GET['Q'];
$result = mysql_query($query, $conn) or die (mysql_error());
echo "<select name =\"gun\">";
while($row = mysql_fetch_array($result)) {
echo "<option value =\"{$row['gunID']}\">".$row['gunName']."</option>";
}
echo "</select>";
} else {
echo "Sorry you do not have access to this page.";
}
?>

emehrkay
06-25-2005, 11:12 PM
i don think you need php for this because i seen a site earlier that let you do it without refreshing the page, but didnt save the link sorry.

you have you initial jump menu that has to be poplulated
<option value = xxx.php?q=blah>
and that menu's action will refresh the page with variables being passed in the url

the scond menu will pull those variables. you have the first menu doing it



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum