Use an allow-list for accepted option values. The same validated value can then be used to mark the matching option as selected.
$allowed = ['one', 'two', 'three'];
$selected = $_POST['class'] ?? '';
if (!in_array($selected, $allowed, true)) {
$selected = '';
}
echo '<option value="two"' . ($selected === 'two' ? ' selected' : '') . '>Two</option>';
This is the recommended starting pattern. The detailed examples below are retained for explanation, comparison and related variations.
| smo1234 | 18-06-2012 |
| thanks this is working fine | |
| Jnanendra Veer | 05-09-2012 |
| thanks this is working fine | |
| Murugavel | 04-08-2014 |
| Working perfectly Dude.Thanks | |