Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Drop down box dependant on another drop down box

Status
Not open for further replies.

Brianfree

Programmer
Feb 6, 2008
220
GB
Hi i have the following working code which i can select a manufacturer from a drop down box. This works fine however i want to create another drop down box called model_type which has data in another recordset.

When i select a value from the manufacturer drop down i want the model_type dropdown to display models for that manufacturer. Hope this makes sense.. !! Ultimatly after selecting a manufacturer and model_type, etcc a unique code will be available to pass to another page..

Code:
<?php require_once('Connections/yardbuying.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_yardbuying, $yardbuying);
$query_Recordset1 = "SELECT Manufacturer FROM vehicles GROUP BY Manufacturer ORDER BY Manufacturer Asc";
$Recordset1 = mysql_query($query_Recordset1, $yardbuying) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

</select>
<form id="form1" name="form1" method="post" action="">
  <label>
    <select name="Manufacturer" id="Manufacturer">
      <?php
do {  
?>
      <option value="<?php echo $row_Recordset1['Manufacturer']?>"><?php echo $row_Recordset1['Manufacturer']?></option>
      <?php
} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));
  $rows = mysql_num_rows($Recordset1);
  if($rows > 0) {
      mysql_data_seek($Recordset1, 0);
	  $row_Recordset1 = mysql_fetch_assoc($Recordset1);
  }
?>
    </select>
  </label>

  </select>
</label>
</form>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>

Hope this is somthing easy to do!

Many thanks,

Brian
 
Yes, and no. If you want the other drop down to be immediately populated, then you'll need Javascript or Ajax at the very least.

If you can live with the original drop down refreshing the page so the other one is populated then this can be done with only PHP.

In pseudo code this would be:

Code:
populate firs drop down.


If(dropdwown1 is submitted)
{
use value to generate second drop down contents.

display second drop down.
}

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Hi thanks for replying.. Not sure which is the best way to do this. I have a table with over 17000 unique vehicles..

WIll need to filter by manufacturer, modeltype, model spec, year from that i can get the vehicle id.

Which way would you suggest - i am a little new to PHP & mysql?

Many thanks,

Brian
 
my view is that you should filter server side. delivering that number of options per page refresh to the browser, and relying on client-side js filtering i think would not be sensible.

from a UI/UE point of view, I feel that ajax interaction to the server side filtering script would be best. but you might start with page-refresh based filtering, get your script working and then migrate to ajax.
 
Its more appealing to users to have it happen immediately. However I almost never use JS in my pages, so I choose the page refresh. In most cases its less than a second's wait to get the populated drop down back.







----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thanks for all your replies - will have a go first passing the selected value to the second drop down!!!

Does anyone have any example code i could play with like onchange pass value to new recordset query or whatever?

Many thanks,

BF
 
Something like this:

Code:
<form action="dropdown.php" method=POST>
<select name="dropdown1" [red]Onchange="document.forms[0].submit();[/red]">
<option value="0">First Option</option>
<option value="1">Second Option</option>
<option value="2">Third Option</option>
<option value="3">Fourth Option</option>
</select>


<?PHP
if(isset($_POST['dropdown1'])){

echo "<select name='dropdown2'>";

[green]\\Generate second drop down [/green]
?>

</form>

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Hi, does anyone know where i am going wrong on this... The last item in the list is always selected not the item that matches $Manufacturer?

Code:
<?PHP

$Manufacturer = $_POST["Manufacturer"];
echo $Manufacturer;
$Selected = ""
?>
<form action="test.php" method=POST>
 <label>
	<select name="Manufacturer" id="Manufacturer" Onchange="document.forms[0].submit();">
<?php
do {  
	if($Manufacturer = $row_Recordset1['Manufacturer']) {
		$Selected = " selected";
	}
?>
      <option value="<?php echo $row_Recordset1['Manufacturer']?>" <?php echo $Selected?>><?php echo $row_Recordset1['Manufacturer']?></option>
      <?php
} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));
  $rows = mysql_num_rows($Recordset1);
  if($rows > 0) {
      mysql_data_seek($Recordset1, 0);
	  $row_Recordset1 = mysql_fetch_assoc($Recordset1);
  }

?>
    </select>
  </label>

</form>


Many thanks,

BF
 
You are assigning instead of comparing, you need one for equals sign for the comparison:
Code:
if($Manufacturer [red]==[/red] $row_Recordset1['Manufacturer']) {
        $Selected = " selected";




----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top