How do I set the selected item in a drop down box

2019-01-10 23:50发布

Is there any way to set the selected item in a drop down box using the following 'type' code?

<select selected="<?php print($row[month]); ?>"><option value="Janurary">January</option><option value="February">February</option><option value="March">March</option><option value="April">April</option></select>

The database holds a month.. and I want to allow on the edit page, them to choose this month.. but it to be pre-filled with their current setting?

9条回答
Explosion°爆炸
2楼-- · 2019-01-11 00:09

Its too old but I have to add my way as well :) because it is generic and useful especially when you are using static dropdown values.

function selectdCheck($value1,$value2)
   {
     if ($value1 == $value2) 
     {
      echo 'selected="selected"';
     } else 
     {
       echo '';
     }
     return;
   }

and in you dropdown options you can use this function like this and you can use this as many as you can because it fits with all of your select boxes/dropdowns

<option <?php selectdCheck($row[month],january); ?> value="january">january</option>

:) I hope this function help others

查看更多
男人必须洒脱
3楼-- · 2019-01-11 00:11

Simple and easy to understand example by using ternary operators to set selected value in php

<?php $plan = array('1' => 'Green','2'=>'Red' ); ?>
<select class="form-control" title="Choose Plan">
<?php foreach ($plan as $id=> $value) { ?>
  <option value="<?php echo $id;?>" <?php echo ($id==  '2') ? ' selected="selected"' : '';?>><?php echo $value;?></option>
<?php } ?>
</select>
查看更多
SAY GOODBYE
4楼-- · 2019-01-11 00:11

Simple way

<select class ="dropdownstyle" name="category" selected="<?php print($messageeditdetails[0]['category_id']); ?>">

<option value=""><?php echo "Select"; ?></option>

<?php  foreach ($dropdowndetails as $dropdowndetails) { ?>
    <option <?php if($messageeditdetails[0]['category_id'] == $dropdowndetails['id']) { ?> selected="<?php echo $dropdowndetails['id']; ?>" <?php } ?> value="<?php echo $dropdowndetails['id']; ?>"><?php echo $dropdowndetails['category_name']; ?></option>
<?php } ?>
</select>
查看更多
The star\"
5楼-- · 2019-01-11 00:13

You need to set the selected attribute of the correct option tag:

<option value="January" selected="selected">January</option>

Your PHP would look something like this:

<option value="January"<?=$row['month'] == 'January' ? ' selected="selected"' : '';?>>January</option>

I usually find it neater to create an array of values and loop through that to create a dropdown.

查看更多
时光不老,我们不散
6楼-- · 2019-01-11 00:22

You mark the selected item on the <option> tag, not the <select> tag.

So your code should read something like this:

<select>
    <option value="January"<?php if ($row[month] == 'January') echo ' selected="selected"'; ?>>January</option>
    <option value="February"<?php if ($row[month] == 'February') echo ' selected="selected"'; ?>>February</option>
    ...
    ...
    <option value="December"<?php if ($row[month] == 'December') echo ' selected="selected"'; ?>>December</option>
</select>

You can make this less repetitive by putting all the month names in an array and using a basic foreach over them.

查看更多
一纸荒年 Trace。
7楼-- · 2019-01-11 00:22

A Simple Solution: It work's for me

<div class="form-group">
    <label for="mcategory">Select Category</label>
    <select class="form-control" id="mcategory" name="mcategory" required>
        <option value="">Please select category</option>
        <?php foreach ($result_cat as $result): ?>
        <option value="<?php echo $result['name'];?>"<?php 
           if($result['name']==$mcategory){
               echo 'selected';
           } ?>><?php echo $result['name']; ?></option>
                                        }
        <?php endforeach; ?>
    </select>
</div>
查看更多
登录 后发表回答