How can the PHP code be modified to display the selected value in the select box?
To display the selected value in the select box, you can use the "selected" attribute in the option tag for the value that should be selected by default. This attribute will make that option pre-selected when the page loads.
<select name="color">
<option value="red" <?php if(isset($_POST['color']) && $_POST['color'] == 'red') echo 'selected'; ?>>Red</option>
<option value="blue" <?php if(isset($_POST['color']) && $_POST['color'] == 'blue') echo 'selected'; ?>>Blue</option>
<option value="green" <?php if(isset($_POST['color']) && $_POST['color'] == 'green') echo 'selected'; ?>>Green</option>
</select>