SSG SSG - 3 years ago 343
MySQL Question

Keep selected option in php

I have below code which runs query correctly but does not keep selected option. I could get similar answers but not worked with this code. Here select options are in combination of user given and rest are from mysql column.
I am new to php, so any help will be appreciated.

<select name="tester">

<option value=""> -----------ALL----------- </option>

$dd_res=mysqli_query($conn, "Select DISTINCT tester from workflow1");
echo "<option value='$r[0]'> $r[0] </option>";
<input type="submit" name="find" value="find"/>

Answer Source

You can use the $_POST superglobal array to check the value of submitted form fields. In this case $_POST['tester'] contains the value of the select field after submitting the form.

echo "<option value='$r[0]'".($_POST['tester']==$r[0]?' selected':'')."> $r[0] </option>";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download