PDA

View Full Version : 3 Dynamic Dropdowns


AMMoyer
05-06-2008, 04:10 PM
I've been through DavidJ's tutorials on the subject several times and I'm sure I'd not be as far along without them. I greatly appreciate the assistance. I'm actually doing an automotive catalog lookup for my company, so the example is perfect. I've got a drop down with Years, Make and Model. It works great except after the Year and Make have been input, I get no choices in my Model dropdown.


<?php
mysql_connect("localhost","root","root") or die(mysql_error());
mysql_select_db("applications") or die(mysql_error());
$year = $_POST['year2'];
if ($year){
/////////////////////////////////////////////////
$query = sprintf("SELECT DISTINCT makename FROM iemmy WHERE yearid='$year'");
$result = @mysql_query($query);
$rowMake = mysql_fetch_array($result);
/////////////////////////////////////////////////
}
?>
<?php
$make = $_POST['make'];
if ($make){
/////////////////////////////////////////////////
$query2 = sprintf("SELECT DISTINCT modelname FROM iemmy WHERE makename='$make'");
$result2 = @mysql_query($query2);
$rowModel = mysql_fetch_array($result2);
/////////////////////////////////////////////////
}
?>
<!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" />
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="dropdown.php" >
<select name="year2" onchange="document.forms[0].submit()">
<option value="" selected="selected">Select Year</option>
<option value="2009" <?PHP if (!(strcmp(2009, $year))){echo "selected";}?>>2009</option>
<option value="2008" <?PHP if (!(strcmp(2008, $year))){echo "selected";}?>>2008</option>
<option value="2007" <?PHP if (!(strcmp(2007, $year))){echo "selected";}?>>2007</option>
<option value="2006" <?PHP if (!(strcmp(2006, $year))){echo "selected";}?>>2006</option>
<option value="2005" <?PHP if (!(strcmp(2005, $year))){echo "selected";}?>>2005</option>
<option value="2004" <?PHP if (!(strcmp(2004, $year))){echo "selected";}?>>2004</option>
<option value="2003" <?PHP if (!(strcmp(2003, $year))){echo "selected";}?>>2003</option>
<option value="2002" <?PHP if (!(strcmp(2002, $year))){echo "selected";}?>>2002</option>
<option value="2001" <?PHP if (!(strcmp(2001, $year))){echo "selected";}?>>2001</option>
<option value="2000" <?PHP if (!(strcmp(2000, $year))){echo "selected";}?>>2000</option>
</select>
<select name="make">
<option value="">Select Make</option>
<?php do { ?>
<option value="<?php echo $rowMake['id']; ?>"><?php echo $rowMake['makename']; ?></option>
<?php }while ($rowMake = mysql_fetch_array($result)); ?>
</select>
<select name="model">
<option value="">Select Model</option>
<?php do { ?>
<option value="<?php echo $rowModel['id']; ?>"><?php echo $rowModel['modelname']; ?></option>
<?php }while ($rowModel = mysql_fetch_array($result)); ?>
</select>
</form>
</body>
</html>


I think there's an issue with the $_POST for the Make not "getting" into the query. I've tried putting both queries & POST statements in the first PHP statement, but got the same results. How do I check to see if the POST is catching the data? I'm very green with PHP, any help is appreciated.

Adam

davidj
05-06-2008, 06:06 PM
echo the query to see if it looks correct

echo $query2 = sprintf("SELECT DISTINCT modelname FROM iemmy WHERE makename='$make'");

AMMoyer
05-06-2008, 06:50 PM
Thanks again. For starters I was not querying both the MakeID and the Makename from the db.

After stepping through things, the "make" is not getting sent to the $_POST['make'] after the Make is selected. I can echo the year and it checks out but not the Make.

davidj
05-06-2008, 07:04 PM
you dont have a javascript call on the make select to submit the information


onchange="document.forms[0].submit()">

this is why its not getting a value