How to display selected item at drop down list?

橙三吉。 提交于 2019-12-08 10:42:53

问题


I am currently working on a edit form with drop down list. I have a edit form that display current item from database. But I have no idea how my drop down menu to display current value from database that match drop down list. I know my explanation a bit confuse therefore I picture below will show better explanation.

This is one of my edit form that showing drop down menu. What I want is display database value with this view of drop down menu.

Example :

My database value is Pending. Then it will show Pending inside this view of drop down menu. And when I click on the drop down menu, it will show me like below picture :

this is what I am looking for. And below is the code that I tried.

<div class="floatleft">
    <select name="select2" class="select-form-standard" >
         <option value="0" id="0" name="status">Deleted</option>
         <option value="1" id="1" name="status">Active</option>
         <option value="2" id="2" name="status">Pending</option>
         <option value="2" id="2" name="status">Suspended</option>
    </select>
</div>

But I just know how to design a drop down menu and do not have idea how to work like this. I am working with smarty framework. Can someone give me some solution to work on it? Thanks in advanced.


回答1:


Add selected attribute for the option which must be selected. Assume $value = 0

<?php $value = 0; ?>
<select name="select2" class="select-form-standard" >
    <?php foreach($options as $id => $option) { ?>
        <option value="<?php echo $id ?>"<?php 
            if($id == $value) { 
            echo " selected"; 
            } ?>><?php echo $option ?></option>
    <?php } ?>
</select>



回答2:


Try this:

$statusval is variable coming from your database

    <option value="0" id="0" <?php if($statusval==0){echo "selected;"}?>  name="status">Deleted</option>
     <option value="1" id="1" <?php if($statusval==1){echo "selected;"}?> name="status">Active</option>
     <option value="2" id="2" <?php if($statusval==2){echo "selected;"}?> name="status">Pending</option>
     <option value="2" id="2" <?php if($statusval==3){echo "selected;"}?> name="status">Suspended</option>



回答3:


<?php
$databaseValue = $your_database_value;
?>    

<select name="select2" class="select-form-standard" >
 <option value="0" <?php echo (($databaseValue==0)?"selected":"") ?> id="0">Deleted</option>
 <option value="1" <?php echo (($databaseValue==1)?"selected":"") ?> id="1">Active</option>
 <option value="2" <?php echo (($databaseValue==2)?"selected":"") ?> id="2">Pending</option>
 <option value="3" <?php echo (($databaseValue==3)?"selected":"") ?> id="3">Suspended</option>
</select>



回答4:


Try this

/**
     * Takes To values (First for option value Second for value to be compare)
     * @param Srting $option stores Option Value String
     * @param String $value Stores to be compare
     * @return String
     * @access public
     */
function selectBoxSelection($option, $value) {
        if ($option == $value) {
            return "selected";
        }
    }

<select name="status">
<option value style='display:none;'>-SELECT Status--</option>
$select_data=mysql_query("Select status,options from yourtable");
while($result=mysql_fetch_array($select_data)){
echo "<option value ='YourValue' ".selectBoxSelection('YourOptionValue', $result['status']).">".$resule['option']."</option>";
}
</select>



回答5:


You need to add the attribute "selected" to the option you wish to have preselected:

<option value="2" id="2" name="status" selected>Pending</option>

or if you are using xhtml you will need to use

selected="selected"

To have this work off a value in database you will need to write an if statement to determine if the database value is the same as the option value, and if it is, then echo out the "selected" attribute within the option tag.




回答6:


you can easily do it by a simple if statement. inside your loop try this

<option value="<?= $row['id']) ?> " <?= $row['id']==$categoryIDfromDatabase?'selected':'';  ?> > 
<?= $row['categoryName'] ?>
</option>



回答7:


<select name="select2" class="select-form-standard" >
<option><?php echo $select2e; ?></option>
<option value="0" id="0" name="status">Deleted</option>
<option value="1" id="1" name="status">Active</option>
<option value="2" id="2" name="status">Pending</option>
<option value="2" id="2" name="status">Suspended</option>
</select>


来源:https://stackoverflow.com/questions/24257490/how-to-display-selected-item-at-drop-down-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!