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


Copy code

The code is as follows:

//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’ : ”
<input type=”submit” value=”submit” />

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