PDA

View Full Version : dynamic dropdown


mr_badger
11-16-2012, 03:41 PM
Hi,
I have gone through the dynamic dropdown tutorial and managed to get it working but would like to know how to add a third/fourth dropdown menu, I have tried but cannot get it working right. I have my code below and I would like to add a number of bedrooms to the dropdown lists.



<?php require_once('Connections/property.php');


$query_regions = "SELECT * FROM region";
$regions = mysql_query($query_regions, $property) or die(mysql_error());
$row_regions = mysql_fetch_assoc($regions);



$region = $_POST['regions'];

if ($region) {

/////////////////////////////////////////////////////////////////////////////////
$query_towns = sprintf("SELECT * FROM town WHERE region_id = '$region'");
$towns = mysql_query($query_towns) or die(mysql_error());
$row_towns = mysql_fetch_array($towns);
/////////////////////////////////////////////////////////////////////////////////
}



?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Dynamic Dropdown Test</title>
</head>

<body>

<form id="search" action="test_search.php" method="post" name="search">

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

<option value="" selected>Select Region</option>

<?php do { ?>

<option value="<?php echo $row_regions['region_id']?>"><?php echo $row_regions['region']?></option>


<?php
} while ($row_regions = mysql_fetch_array($regions));
?>

</select>



<select name="towns">



<?php do { ?>


<option value="<?php echo $row_towns['town_id']?>"><?php echo $row_towns['town']?></option>

<?php } while ($row_towns = mysql_fetch_array($towns)); ?>


</select>




</form>


</body>
</html>





Also I'm having a problem that if you select the default selected "Select Region" I get an error in the second dropdown (!) Notice: Undefined Variable: row_towns on line 59


Hope someone can help me with this as I'm sure it's very simple to do but I'm no php expert.

Thankyou.

jmichae3
12-11-2012, 05:36 AM
<?php require_once('Connections/property.php'); ?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Dynamic Dropdown Test</title>
</head>
<body>
<form id="search" action="test_search.php" method="post" name="search">
<select name="regions" onchange="document.forms[0].submit()">
<option value="" selected>Select Region</option>
<?php
$query_regions = "SELECT * FROM region";
$regions = mysql_query($query_regions, $property) or die(mysql_error());
$num_regions = mysql_num_rows($regions); //used for detection of success
while ($row_regions = mysql_fetch_array($regions)) {
echo "<option value=\"".$row_regions['region_id']."\">".$row_regions['region']."</option>\n";
}
mysql_free_result($query_regions); //ALWAYS free result after you are done with it! or you get wacky results!
?>
</select>

<select name="towns">
<?php
if ($num_regions >= 1) {
$query_towns = "SELECT * FROM town WHERE region_id = '".$region."'";
$towns = mysql_query($query_towns) or die(mysql_error());
$row_towns = mysql_fetch_array($towns);
while ($row_towns = mysql_fetch_array($towns)) {
echo "<option value=\"".$row_towns['town_id']."\">".$row_towns['town']."</option>\n";
}
mysql_free_result($query_towns); //ALWAYS free result after you are done with it! or you get wacky results!
}
?>
</select>
</form>
</body>
</html>