SSG SSG - 11 months ago 159
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>
<?php

$dd_res=mysqli_query($conn, "Select DISTINCT tester from workflow1");
while($r=mysqli_fetch_row($dd_res))
{
echo "<option value='$r[0]'> $r[0] </option>";
}
?>
</select>
<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