How do i write php code for getting field from mysql as dropdown in php, and, also to display the related records of selected option in dropdown from database

 $query = "SELECT `id`, `name` FROM `tableName`";
 $result = mysql_query($query) or die(mysql_error()."[".$query."]"); 

<select name="dropdown">
       <?php 

 while ($row = mysql_fetch_array($result))
 {
     echo "<option value=".$row['id']."'>".$row['id'] . " - " . $row['name']."</option>";
 }
 ?>
 </select>

Thank you, but this displays only the option from databse into dropdown but i need to fetch the id and name from databse related to the selected option

Member Avatar for iamthwee

You need to use jquery/ajax so when you select an option from the dropdown to filters on this information

how to use ajax/jquery to do that

Member Avatar for iamthwee
$.ajax({
      url: 'link_to_url',
      type: 'post',
      data: {},
      success: function (data) {
       //update dom here
      }
    });

This is what I use, you can edit the table names and fields to match your database.

<?php $result = mysql_query("Select birthstate.state_id AS menubstate_id, birthstate.state_abbrev AS menubstate_name FROM birthstate Having menubstate_id > '0' Order By menubstate_name Asc"); ?>
<select name="bstate" id="bstate">
<option value="<?php echo $myrow["bstate"]?>"><?php echo $birthstate;?></option>
<?php while($row = mysql_fetch_array($result)) { ?>
<option value="<?php echo $row["menubstate_id"]?>"><?php echo $row['menubstate_name'];?></option>
<?php } ?>

this should work

<?php 

if($_GET['id']) {

$id = $_GET['id'];
$data = mysql_query("SELECT * FROM table_name WHERE id='$id' ") 
or die(mysql_error()); 
while($info = mysql_fetch_array( $data ))

{   ?>

<select onChange="if(this.selectedIndex!=0)
self.location=this.options[this.selectedIndex].value" class="search-select" >
<option value="?id=<?php print "".$info['id'] ."";  ?>" ><?php print "".$info['columb_name'] ."";  ?>
</select>


<?php print "".$info['id'] ."";  ?> - <?php print "".$info['name'] ."";  ?>


<?php } } else if(!$_GET['id']){ ?>

<?php
$data = mysql_query("SELECT * FROM table_name ") 
or die(mysql_error()); 
while($info = mysql_fetch_array( $data ))

{   ?>

<select onChange="if(this.selectedIndex!=0)
self.location=this.options[this.selectedIndex].value" class="search-select" >
<option value="?id=<?php print "".$info['id'] ."";  ?>" ><?php print "".$info['columb_name'] ."";  ?>
</select>
<?php } ?>


<?php } ?>

yes this is working but it displays only the option in dropdown, i also need to fetch the data from database depending on the selected dropdown

It should initially show the options but when you select one it will reload the page with relevant data

Sry, this doesnot fetch relevent data, plz help me

Thank you all, Finally i got it

use pdo_mysql also:

   use it below code for database connection & drop down:

   <?php
    # We are storing the information in this config array that will be required to connect to the database.
    $config = array(
    'host'  => 'localhost',
    'username'  => 'username',
    'password'  => 'password',
    'dbname' => 'dbname'
    );
    try{
    #connecting to the database by supplying required parameters
    $conn = new PDO('mysql:host=' . $config['host'] . ';dbname=' . $config['dbname'], $config['username'], $config['password']);
     }
     catch(PDOException $pe)
{
  die('Connection error, because: ' .$pe->getMessage());
}

    #Setting the error mode of our db object, which is very important for debugging.
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    ?>

<form name="name_of_the_form" action="index.php" method="post">
<select name="dropdown" selected="selected">
<option value="">--Select Value--</option>
<?php
$query = $conn->prepare("select * from table_name ORDERBY table_id ASC");
$query->execute();
$count= $query->count();
for($i=0; $result=$query->fetch(); $i++)
{
echo '<option value="'.$result['dropdown_index_value'].'">'.$result['dropdown_index_name'].'</option>';
}
?>
</select>
</form>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.