Go Back   Dreamweaver Club Forums > Hand Coders Forum > PHP
Register FAQ Members List Search Today's Posts Mark Forums Read

Thread Tools Display Modes
Old 11-16-2012, 02:41 PM   #1
Join Date: Dec 2006
Posts: 84
Default dynamic dropdown

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 Code:

<?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);

<meta charset="utf-8">
<title>Dynamic Dropdown Test</title>


<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>

} while ($row_regions mysql_fetch_array($regions));


<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)); ?>



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.

mr_badger is offline   Reply With Quote
Old 12-11-2012, 04:36 AM   #2
Join Date: Dec 2010
Posts: 366

<?php require_once('Connections/property.php'); ?>
<meta charset="utf-8">
<title>Dynamic Dropdown Test</title>
<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>
$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 name="towns">
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!
Jim Michaels
HTML Code:
browser confusion (I believe the term is 'tag soup')!
jmichae3 is offline   Reply With Quote

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT. The time now is 04:55 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2018, vBulletin Solutions, Inc.
Copyright 2006 DreamweaverClub.com