View Full Version : Dynamic Drop Down Help

02-05-2008, 06:28 PM
I've got my dynamic drop down menu working. However, I need help get the results page linked properly. When the final drop down menu is selected, the form sends you to the correct page (i.e. product.php?product_id=), but the url parameter is always = 4. Basically the same page pops up no matter what is selected in the final drop down.

Here is the code:

require_once('Connections/connUser.php'); //database connection

mysql_select_db($database_connUser, $connUser);
$query = "SELECT * FROM manufacturers ORDER BY manufacturers.manufacturer_name";
$result = @mysql_query($query) or die(mysql_error());
$rowMan = mysql_fetch_array($result);

//////////////// Set Variables to Dummy if not Set//////////
if (!isset($_POST['manufacturer'])) {
$_POST['manufacturer'] = "undefine";
if (!isset($_POST['type'])) {
$_POST['type'] = "undefine";
if (!isset($_POST['name'])) {
$_POST['name'] = "undefine";
if (!isset($_GET['model'])) {
$_GET['model'] = "undefine";
////////////////////End Setting Dummies///////////////////////

$manufacturer = $_POST['manufacturer'];
$type = $_POST['type'];
$name = $_POST['name'];
$product_id = $_GET['model'];

if (isset($manufacturer)){
$query_man = sprintf("SELECT * FROM product_type WHERE manufacturer_id='$manufacturer' ORDER BY product_type.type");
$result_man = @mysql_query($query_man);
$rowType = mysql_fetch_array($result_man);

if (isset($type)){
$query_type = sprintf("SELECT * FROM product_name WHERE type_id='$type' ORDER BY product_name.name");
$result_type = @mysql_query($query_type);
$rowName = mysql_fetch_array($result_type);

if (isset($name)){
$query_name = sprintf("SELECT * FROM product WHERE name_id='$name' ORDER BY product.model_name");
$result_name = @mysql_query($query_name);
$rowModel = mysql_fetch_array($result_name);

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<form id="form1" form name="form1" method="post" action="dropdown.php">
<select name="manufacturer" onChange="document.forms[0].submit()">
<option value="">Select Manufacturer</option>
<?php do { ?>
<option value="<?php echo $rowMan['manufacturer_id']; ?>"<?php if (!(strcmp($rowMan['manufacturer_id'], $manufacturer))) {echo "SELECTED";} ?>><?php echo $rowMan['manufacturer_name']; ?></option>
<?php }while ($rowMan = mysql_fetch_array($result)); ?>

<select name="type" onChange="document.forms[0].submit()">
<option value="">Select Product Type</option>
<?php do { ?>
<option value="<?php echo $rowType['type_id']; ?>"<?php if (!(strcmp($rowType['type_id'], $type))) {echo "SELECTED";} ?>><?php echo $rowType['type']; ?></option>
<?php }while ($rowType = mysql_fetch_array($result_man)); ?>

<select name="name" onChange="document.forms[0].submit()">
<option value="">Select Product Name</option>
<?php do { ?>
<option value="<?php echo $rowName['name_id']; ?>"<?php if (!(strcmp($rowName['name_id'], $name))) {echo "SELECTED";} ?>><?php echo $rowName['name']; ?></option>
<?php }while ($rowName = mysql_fetch_array($result_type)); ?>

<select name="model" onChange="document.location.href='product.php?product_id=<?php echo $rowModel['product_id']?>'">
<option value="">Select Model Number</option>
<?php do { ?>
<option value="<?php echo $rowModel['product_id']; ?>"<?php if (!(strcmp($rowModel['product_id'], $product_id))) {echo "SELECTED";} ?>><?php echo $rowModel['model_name']; ?></option>
<?php }while ($rowModel = mysql_fetch_array($result_name)); ?>


Any suggestions???

02-08-2008, 10:21 AM
did you get this sorted

sorry i missed it

02-08-2008, 04:56 PM
I'm stilll working on it. First off, your tutorials are great. I have a pretty good idea how to do some basic coding, but understanding WHY you write the code a certain way is very empowering. I've relearned quite a few things from this site and hope to be able to contribute my own knowledge to some of these posts.

Anyway, I'm still searching for the right way to link the final drop down to a results page. Rather than shooting in the dark, Could you direct me a little.

Thanks in advance.

02-08-2008, 06:37 PM
ok you have this

<select name="model" onChange="document.location.href='product.php?product_id=<?php echo $rowModel['product_id']?>'">

what your doing is passing the product_id=<?php echo $rowModel['product_id']?> as part of your value pair and i can see your logic here

what your doing wrong is your assigning it on the select itsself which is outside the loop which means it will always return the same value (the first it finds in the table)

ok what we need to do here is write a javascript function which you will call

<script language="javascript" type="text/javascript">

function passData(){

f = document.getElementsByName('model').value; //<< assign field value to f var

document.location.href='product.php?product_id='+f ;



then you just call the function like this...

<select name="model" onChange="passData()">

02-09-2008, 05:36 AM
I've writen the javascript function into my drop down and I understand why we need to pass the field value to the url. I am assuming I need to add a bit of javascript to my results page, which will define the variable in that page and submit the variable to page on load??

02-09-2008, 08:11 AM
sorry my fault

let me see what your wanting to do

I can see your passing the values from the other dropdowns into SQL SELECT's but i cant see what they do after the result returns?

ok scrap what i gave you. Ditch the JS and we'll use PHP

what you need to do is ....

<select name="model" id="model" onChange="document.forms[0].submit()">

then catch the value (as you have done before) using a $_POST

$model = $_POST['model'];

then your condition and use a header location




this will pass the option value to the product page.

02-11-2008, 12:42 PM
the only problem is that the $model variable gets set to the dummy value and passes that to the results page on the first submit (i.e. when you select the first drop down)

when I remove the dummy value code and shut off the errors with @$model, the page passes no value to the results page (which loads, but is blank)

basically, the first time onChange="document.forms[0].submit()"
occurs the results page loads with no value

my results page is set up using
$sql="SELECT * FROM product where product_id = '".$_REQUEST['product_id']."'";

so basically, whatever value is passed in the url triggers the page display. The only thing connecting the two pages is the value passed through the url.

02-11-2008, 08:43 PM
still trying to get this worked out

02-14-2008, 03:05 PM

Any ideas with this? I am still trying to work out how to get around this issue.


02-18-2008, 12:12 PM
I have the two pages linked (the drop down form page and results page), but I cannot get what's selected in the last drop down field to populate in the url of the results page.

When an item in the first drop down in the form is selected (there are 4 drop down fields). The drop down page submits a dummy value to the results page. If you look at the onClick for the fourth drop down, it is set to submit to the form itself and there is a conditional statement that catches the selection from the fourth drop down and submits it to the url in the results page.

As I mentioned, this conditional statement is catching the dummy value before the user has selected an item from the fourth drop down.

02-18-2008, 12:51 PM

im on the case

will look at this tonight