hrishi hrishi - 22 days ago 17
PHP Question

echo selected in chosen select

I am using chosen select drop down to show auto complete drop down. I want to set selected value for edit. I tried following code which works for normal select option but not working for chosen select

<select class="chosen-select" >
<option value=""></option>
<?php if(!empty($list))
{
foreach($list as $d)
{
?>
<option value="<?php echo $d->id; ?><?php if($d->id == 2) { echo "selected"; } ?>"><?php echo $d->name; ?></option>
<?php } } ?>
</select>

Answer

You are putting your selected inside your value attribute, you need to write it after :

       <select class="chosen-select" >
              <option value=""></option>
                <?php if(!empty($list)) {
                        foreach($list as $d) {
                ?> 
                    <option value="<?php echo $d->id; ?>"<?php if($d->id == 2) { echo " selected"; } ?>><?php echo $d->name; ?></option>
              <?php  } } ?>
       </select>
Comments