Jump to content

Dynamic Dropdown Lists


pbrandy22

Recommended Posts

Hi everyone,

 

I've read through the FAQs for Dynamic Dropdown Menus on this site, as well as others, and I can't figure out why my code won't work. I have two dropdown boxes that need to be populated with data from a mysql table; one menu for route types, and one for route numbers. When I choose the route from the 'Route' menu, the 'Number' menu automatically populates with all of the possible numbers, rather than only those that correspond with the route type. I can tell that the problem has something to do with the value of 'Route' not being recognized, but I don't know why. I'm a beginner when it comes to PHP, so any suggestions or help would be much appreciated!  Thanks!

 

The code is as follows:

 


<html> 
<body> 
<basefont face='calibri' color='#7E2217'> 
<?php 
// set variables 
$mileTable = $_GET['mileTable']; 
$routeType = isset($_POST['Roadtype'])? $_POST['Roadtype']: 0; 
include 'opendbMile.php'; 
include 'error.php'; 
    // Connect to the MySQL DBMS 
   if (!($connection = @ mysql_connect($hostName, 
                                                                           $username, 
                                                                           $password))) 
          die("Could not connect"); 
   if (!mysql_select_db($databaseName, $connection)) 
         showerror(  ); 

    // Start a query... 
    $query = "SELECT ID, 
                                         Roadtype 
                          FROM Alabama 
                          GROUP BY Roadtype"; 
    // execute the SQL statement 
    $result = mysql_query($query, $connection) or die(mysql_error()); 
echo '<form name="mileform" method="post" action="MileQuery.php">'; 
echo '<p>Route: 
<select name="routeType" id="routeType" onchange="this.form.submit();"> 
<option value="0"'.($routeType == 0? ' SELECTED': '').'>Route</option>'; 
while($row = mysql_fetch_array($result)){ 
    echo ' 
            <option value="'.$row[0].'"'.(($routeType == $row[0])? ' SELECTED': '').'>'.$row[1].'</option>'; 
} 
    echo ' 
    </select> 
</p>'; 
// create the SQL statement 
$query2 = "SELECT ID, 
          Roadnumber 
                   FROM Alabama 
                   GROUP BY Roadnumber"; 
         if($mnucategory != 0){    // Filter road numbers 
    $query2 .= " WHERE Roadtype='".$routeType."'"; 
    } 
// execute the SQL statement 
$result2 = mysql_query($query2, $connection) or die(mysql_error()); 
echo '<p>Number: 
    <select name="routeNumber" id="routeNumber" onchange="this.form.submit();"> 
            <option value="0"'.($routeNumber == 0? ' SELECTED': '').'>Number</option>'; 
while($row2 = mysql_fetch_array($result2)){ 
    echo ' 
            <option value="', $row2[0].'"'.(($routeNumber == $row2[0])? ' SELECTED': '').'>'. $row2[1], '</option>'; 
} 
    echo ' 
    </select> 
</p>'; 
// Close the DBMS connection 
   mysql_close($connection); 
?> 
</form> 
</body> 
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.