javascript 如何在表单提交后保持动态下拉框的值处于选中状态?

ckx4rj1h  于 2023-05-21  发布在  Java
关注(0)|答案(2)|浏览(89)

请帮助我在表单提交后显示选定的电子邮件。请在下面提到的代码上有一个:

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

ilmyapht1#

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

xpcnnkqh2#

你可以通过选择下拉选项来解决这个问题
试试这个

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

希望这对你有帮助

相关问题