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?
You search for
I recommend you to read through some HTML beginner guides.
Try this as an example.
Put selected in for the option you want to be selected.
Like:
When you build the above list, for each option you check whether it is the one in the database. And when it is, you add selected.
You need to use the
selected
attribute in the<option>
HTML tag.Full code:
You need to use the
selected
attribute inHTML
.If for example the value you got from the database is assigned to a variable say
$val
,Then you can do it as follows :