douzhuo5671
2018-07-19 21:20
浏览 97
已采纳

PHP如果变量等于下拉列表中的值,则将“selected”属性添加到选项中

I have a drop down menu that checks whether a user has already selected a value by checking database and if they have, I want to add a 'selected' attribute to that option so that when they edit their profile, that option is preselected by what they chose.

Heres an example of what I am trying to accomplish. It works for text inputs but I don't know how to do it with dropdown lists.

So if user selects 'Dog', it gets place in database and adds 'selected' as attribute

$animal = $mysqli->escape_string($_POST['animal']);
//PHP UPDATE database script -------->

<label>Animal</label></br>
    <select name='animal' value='<?php if($animal == value){ /*Add selected attribute to option */ ?>'>
         <option value="" disabled selected>Select One</option>
         <option value="" disabled>----------------</option>
         <option value="Dog">Dog</option>
         <option value="Cat">Cat</option>
         <option value="Bird">Bird</option>  
    </select>

2条回答 默认 最新

相关推荐 更多相似问题