0

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?

1

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.