How can the issue of a select box resetting to the first value after selection be addressed in PHP?

Issue: The select box resetting to the first value after selection can be addressed by using PHP to store the selected value in a session variable and then setting the selected attribute in the HTML option tag based on the stored value. PHP code snippet:

<?php
session_start();

$selectedValue = isset($_SESSION['selected_value']) ? $_SESSION['selected_value'] : '';

if(isset($_POST['select_box'])){
    $_SESSION['selected_value'] = $_POST['select_box'];
}

?>

<form method="post">
    <select name="select_box">
        <option value="option1" <?php echo ($selectedValue == 'option1') ? 'selected' : ''; ?>>Option 1</option>
        <option value="option2" <?php echo ($selectedValue == 'option2') ? 'selected' : ''; ?>>Option 2</option>
        <option value="option3" <?php echo ($selectedValue == 'option3') ? 'selected' : ''; ?>>Option 3</option>
    </select>
    <input type="submit" value="Submit">
</form>