Skip to content Skip to sidebar Skip to footer

Retrieve Options For The Second Drop Down Box From Db Based On First Drop Down Box

I am currently working on a small project. I have to retrieve some data from a DB (MySQL) and insert it into a webpage as a select (Drop Down box). The code I have written in PHP i

Solution 1:

Echo your end select out of the loop.

while ($row = mysqli_fetch_array($r)) 
 {
      echo'<option value="'.$row['employee_name'] . '>"'.$row['employee_name'] .'</option>';

 }
 echo"</select>";

Solution 2:

Use this:

if ($r = @mysqli_query ($dbc, $q))
{
    echo'select ....

As $r is different to true on SELECT queries.

EDIT

You are closing the select tag on every iteration of the while. Try it like this:

if ($r = @mysqli_query ($dbc, $q))
{ 
     echo'<select name="employee_name">';
     // Fetch and print all the records:while ($row = mysqli_fetch_array($r)) 
     {
          echo'<option value="'.$row['employee_name'] . '>"'.$row['employee_name'] .'</option>';
     }
     echo"</select>";                         
//   ^//   |__  Now the </select> is out of the loop
}

Post a Comment for "Retrieve Options For The Second Drop Down Box From Db Based On First Drop Down Box"