6
Contributors
12
Replies
44
Views
3 Years
Discussion Span
Last Post by fireburner29
0
 $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>
0

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

0

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

0

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

Edited by diafol: applied code formatting

0

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

Edited by mattskills: typo

0

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

0

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

0

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>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.