每次在下拉列表中显示选定的值

时间:2018-10-30 09:19:03

标签: html forms drop-down-menu

我需要显示一直显示的所选类别值。我有一个类别值列表,并将其添加到数组的下拉列表中。

<form action="#" method="post"> 
    <select name="dropDown" id="drop_down_id">
        <option value=""> Select CATEGORY</option>

<?php
    foreach($decoded as $key => $value ){  
foreach($value as $key1 => $value1 ){
?>  <option value="<?php echo $value1;?>"><?php echo $value1;?></option><?php   
}
}
?>
<input type="submit"  name="submit" value="Submit"/>

</form>

1 个答案:

答案 0 :(得分:0)

我看到您在表单中使用了post,因此您可以使用POST来了解上次选择的内容:

$dropDown=htmlentities($_POST['dropDown'], ENT_QUOTES, "UTF-8");

然后简单地执行make if语句及其完成:

if($dropDown==$value1){$selected='selected';}else{$selected=null;}

结果:

<?php
$dropDown=htmlentities($_POST['dropDown'], ENT_QUOTES, "UTF-8");    
?>  
<form action="#" method="post"> 
    <select name="dropDown" id="drop_down_id">
        <option value=""> Select CATEGORY</option>

        <?php
            foreach($decoded as $key => $value ){  
                foreach($value as $key1 => $value1 ){
                    if($dropDown==$value1){$selected='selected';}else{$selected=null;}
                    echo '<option value="'.$value1.'" '.$selected.'>'.$value1.'</option>';  
                }
            }
        ?>
        <input type="submit"  name="submit" value="Submit"/>

</form>

您也可以将其存储在PHP SESSSION http://php.net/manual/en/function.session-start.php