PHP show drop down selected based on value [closed

2020-01-30 01:32发布

I want to show a drop down selected for some value which is coming from database.

    <select name="plans">
       <option>MAP</option>
       <option>CP</option>
       <option>CPA</option>
       <option>CPF</option>
    </select>

Reading a value from database in PHP/Mysql, lets say "CPA", how do I show this option selected?

7条回答
我只想做你的唯一
2楼-- · 2020-01-30 02:14

Try like this

<select name="plans">
   <option <?php echo ($value == "MAP")?"selected":"" ?> >MAP</option>
   <option <?php echo ($value == "CP")?"selected":"" ?> >CP</option>
   <option <?php echo ($value == "CPA")?"selected":"" ?> >CPA</option>
   <option <?php echo ($value == "CPF")?"selected":"" ?> >CPF</option>
</select>
查看更多
登录 后发表回答