hi....i'm stuck in populate data inside the drop down list. the problem is, when i already success in populating data from db in 1st drop down list, i cannot carry the value. second drop down list data will depends on this value in order to populate data from array into it. same list of array for all the 1st data selected. the third drop down list will display different array for different data selected from the second drop down list. this the my code:

function connecttodb($servername,$dbname,$dbuser,$dbpassword)
global $link;
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!$link){die("Could not connect to MySQL");}
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
//////// End of connecting to database ////////

<!doctype html public "-//w3c//dtd html 3.2//en">


<title>Multiple drop down list box from plus2net</title>
<SCRIPT language=JavaScript>
function reload(form)
var val=form.cat.options[form.cat.options.selectedIndex].value;
self.location='dd.php?cat=' + val ;



please help me....

@$cat=$_GET; // Use this line or below line if register_global is off
if(strlen($cat) > 0 and !is_numeric($cat)){ // to check if $cat is numeric data or not.
echo "Data Error";

$query="SELECT DISTINCT Location FROM serial order by Location";

/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */

echo "<form method=post name=f1 action=''>";
$result = mysql_query ($query);
echo "<select name=cat value=''><Option value=''>Please Select Location</option>";
// printing the list box select command

while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[Location]>$nt[Location]</option>";
/* Option values are added by looping through the array */
echo "</select>";// Closing of list box

echo "<select name=section value=''><Option value=''>Please Select Section</option>";
if(isset($cat) and strlen($cat) > 0){
echo "<option value='METRO'>METRO</option>";
echo "<option value='E-SIDE'>E-SIDE</option>";
echo "<option value='D-SIDE'>D-SIDE</option>";
echo "</select>";// Closing of list box
echo "</form>";
$quer=mysql_query("SELECT DISTINCT subcategory FROM subcategory order by subcategory");


If you want to populate the dropdown boxes without reloading the page, you'll have to use javascript.
Either you use an AJAX request for them, or you load all relevant data in your page and switch the drop down box content using some Javascript functionality.
If you want to do it in PHP only, you will have to re-submit the page when the user selects a list item and then populate the dependent lists in PHP.

Member Avatar


Use code tags - I can't read that.

sori, mayb my code too messy. so, here is the code...

//connect to db
include "config.php";

// Open the form element 
echo '<form action="?" method="post">'; 

//populate data from db
$sql='SELECT DISTINCT Location FROM serial order by Location'; 


echo 'Location : </br>';

while ($row=mysql_fetch_array($result)) { 
  $options.='<option value=\"$location\">'.$location; 

echo '<select name="location" >'; 
echo '<option value=0>- Please select Location -'; 
echo $options; 
echo '</select></br>'; 

$items = array( 
    "METRO" => array("METRO NE", "METRO ODF", "OLT UPLINK"), 
    "E-SIDE" => array("OLT", "ODF", "CABLE", "FDC PANEL", "SPLITTER"),
    "D-SIDE" => array("SP-FDC", "FDC PANEL", "CABLE", "FDP", "SP-FDP") 
// Get the posted data, if any, or initialize as null 
($section = @$_POST['Section']) or $section = null; 
($item = @$_POST['Item']) or $item = null; 
// Print first select 
echo 'Section : </br>';
echo '<select name="Section" onchange="submit();">'; 
if (isset($location) or strlen($location)) { 
  echo '<option value="">- Please select Section -</option>'; 
  foreach(array_keys($items) as $_section) { 
      // Check if this type was selected last submit 
      $selected = ($section == $_section ? 'selected="selected"' : ''); 
      // Print this type as an option 
      echo '<option value="'. $_section .'" '. $selected .'>'. $_section .'</option>'; 
else {
  echo '<option value="" selected="selected">- Please select Section -</option>';
echo '</select></br>'; 
// Print the second select 
echo 'Items : </br>';
echo '<select name="Item" onchange="submit();">'; 
if($section) { 
    echo '<option value="">- Please select Items -</option>'; 
    foreach($items[$section] as $_item) { 
        // Check if this item was selected last submit 
        $selected = ($item == $_item ? 'selected="selected"' : ''); 
        // Print this item as an option 
        echo '<option value="'. $_item .'" '. $selected .'>'. $_item .'</option>'; 
else { 
     echo '<option value="" selected="selected">- Please select Items -</option>'; 
echo '</select>'; 
// Close the form element 
echo '</form>'; 
// Do something with the selected items 
if($section && $item) { 
    echo '<pre>You have selected: '. $section .'->'. $item .'</pre>'; 

after i this code, the $location has no return value. thanks in advance 4 d help

I'm assuming the populating portion to be before line 24. Replace the line 13 - 24 with the following.

echo 'Location : </br>';
echo '<select name="location" >'; 

while ($row=mysql_fetch_assoc($result)) { 
  echo "<option value='$location'>$location</option>"; 

echo '</select></br>';