Gurpreet Kaur
Gurpreet Kaur

Reputation: 77

How to keep the value of dynamic drop down selected after form submission?

Please help me to show the selected email after form submission.Please Have a on the code mentioned below:

<?php
    $query= mysqli_query($conn," SELECT email FROM register");
    $options="";
    $result=mysqli_fetch_assoc($query);
    if(mysqli_num_rows($query) >0)
    {
       while($row=mysqli_fetch_array($query))
       {
         $options = $options."<option>$row[0]</option>";
       }
   }
?>
<select name="email" id="email">
<option value="" disabled="" selected="">SELECT EMAIL</option>
<?php echo $options;?>
</select> 

Upvotes: 0

Views: 76

Answers (2)

Sukh Brar
Sukh Brar

Reputation: 108

    <select name="email" id="email">
    <option value="" disabled="" selected="">SELECT EMAIL</option>
    <?php
    $query= mysqli_query($conn," SELECT email FROM register");
    $result=mysqli_fetch_assoc($query);
    if(mysqli_num_rows($query) >0)
    {
       while($row=mysqli_fetch_array($query))
       {
         echo "<option value='<?=$row[0]?>'><?= $row[0]?></option>";
       }
   }
     ?>
    </select> 

Upvotes: 2

MD. Jubair Mizan
MD. Jubair Mizan

Reputation: 1560

You can solve this problem by selectable your Dropdown option

Try this

<?php
$email = $_POST['email'];
$query= mysqli_query($conn," SELECT email FROM register");
$options="";
$result=mysqli_fetch_assoc($query);
if(mysqli_num_rows($query) >0){
   while($row=mysqli_fetch_array($query)){
     $options.= '<option '.(($row[0]==$email)?'selected="selected"':"").'>'.$row[0].'</option>';
   }
}
?>
<select name="email" id="email">
   <option value="" disabled="" selected="">SELECT EMAIL</option>
   <?php echo $options;?>
</select> 

Hope this will help you

Upvotes: 1

Related Questions