提交表单后如何保持动态下拉列表的选择值?

时间:2019-01-28 10:55:04

标签: javascript php html

请帮助我在提交表单后显示所选电子邮件。请在下面提到的代码上输入:

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

2 个答案:

答案 0 :(得分:1)

您可以通过选择下拉菜单选项来解决此问题

尝试一下

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

希望这对您有帮助

答案 1 :(得分:0)

    <select name="email" id="email">
    <option value="" disabled="" selected="">SELECT EMAIL</option>
    <?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))
       {
         echo "<option value='$row[0]'>$row[0]</option>";
       }
   }
     ?>
    </select>