Reputation: 999
I have this field saved in MYSQL database.
<select id="sel1" name="grpID">
<option value="">select</option>
<option value="G00">option1</option>
<option value="G01">option2</option>
<option value="G02">option3</option>
<option value="G03">option4</option>
</select>
$grpID=$_POST['grpID'];
mysqli_query($db_Conx, "INSERT INTO users (grpID) VALUES ('$grpID')");
NOW, when I load this field, I want the saved value selected in select input box.
For example, if the saved value is "G02", I want the select input field loaded like this ![selection image][1
Is there a efficient way to do this instead of selecting it with if clauses for every and each case?
Upvotes: 0
Views: 1712
Reputation: 1276
1) retrieve $grpID
from database before <SELECT>
tag.
2) Replace html-select with:
<select id="sel1" name="grpID">
<option value="">select</option>
<option value="G00" <?php if($grpID=="G00") echo "selected"; ?> >option1</option>
<option value="G01" <?php if($grpID=="G01") echo "selected"; ?> >option2</option>
<option value="G02" <?php if($grpID=="G02") echo "selected"; ?> >option3</option>
<option value="G03" <?php if($grpID=="G03") echo "selected"; ?> >option4</option>
</select>
OR you can use loop also, if <option>
tags are too many
Upvotes: 2