The HTML drop-down menu keeps the selected value after submission without returning to the default value

Time:2019-11-8

Copy code

The code is as follows:

<html>
<body>
<?php
//Get select value
$select_value = isset($_GET[‘select’]) ? $_GET[‘select’] : ”;
?>
<form action=”?”>
<select name=”select”>
<option value=””>default</option>
<option value=”option1″ <?php
//If the value obtained above is the same as the value in this option,
//Print selected, so that the option is selected by default
echo $select_value == ‘option1’ ? ‘selected’ : ”
?>>option1</option>
</select>
<input type=”submit” value=”submit” />
</form>
</body>
</html>

This script should be added to each option. If there are more than one option, you can print in cycles.