Thursday, June 29, 2017
Home » PHP » How to get selected dropdown in php mysql

How to get selected dropdown in php mysql

How to get selected dropdown in php mysql describe with two types with static drop down list and with database dynamically value in loop,you need to check current value with drop down list value.

Get the selected dropdown static :

<?php 
$country_value = 'india';//selected india
?>
<select country="">
<option value="">Please select</option>
<option value="india" <?php if($country_value=='india') echo "selected"; ?>>India</option>
<option value="usa" <?php if($country_value=='usa') echo "selected"; ?>>USA</option>
</select>

Get the selected dropdown dynamic from database

<?php 
$country_value = '1';//selected first country id 1
?>
<select country="">
<option value="">Please select</option>
<?php $query=mysql_query("Select * from country");
while($data = mysql_fetch_assoc($query)){
 if($data['id']==$country_value)
        $select ="selected";
        else
        $select ='';
 ?>
<option value="<?php echo $data['id']; ?>" <?php echo $select; ?>><?php echo $data['name']; ?></option>
<?php  } ?>
</select>