Keep values selected after form submission

Keep values selected after form submission

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Keep values selected after form submission

When a user submits a GET form, the browser sends the current state of the input fields to the server as query parameters. Upon redirecting the user back to the same URL—often handled by a framework route or a custom script—the server reconstructs the view using those parameters. The challenge arises because standard HTML <select> elements do not automatically highlight an option based solely on its value attribute unless explicitly told to do so during rendering.

In your specific scenario within WordPress, you likely have a redirect occurring after submission that points back to the form page with query arguments like ?name=a&location=y. If your view logic simply iterates over these arguments and generates <option> tags without checking them against the selected state, every page load will display both dropdowns as empty.

To fix this, you must dynamically set the selected attribute on the correct option tag during the rendering phase. This is a fundamental pattern in web development: the server determines the active state based on incoming request data and injects that state into the HTML response.

Implementing Conditional Selection in PHP

The most straightforward approach involves wrapping your loop logic inside an if statement that compares the current option's value against the parameter passed in the URL. Assuming you are rendering a template from within a WordPress theme or plugin, here is how the logic translates to code:

<?php
// Retrieve values from the query string
$name = isset($_GET['name']) ? $_GET['name'] : '';
$location = isset($_GET['location']) ? $_GET['location'] : '';

// Render the 'name' dropdown
echo '<select name="name">';
    
    // Loop through available options
    $options = ['a', 'b']; 
    foreach ($options as $value) {
        // Check if this specific option matches the submitted value
        if ($value === $name) {
            echo '<option value="' . esc_attr($value)" selected>" . esc_html($value) . '</option>';
        } else {
            echo '<option value="' . esc_attr($value) . '">' . esc_html($value) . '</option>';
        }
    }

echo '</select>';

// Render the 'location' dropdown similarly
echo '<select name="location">';
    
    $locations = ['x', 'y'];
    foreach ($locations as $val) {
        if ($val === $location) {
            echo '<option value="' . esc_attr($val)" selected>" . esc_html($val) . '</option>';
        } else {
            echo '<option value="' . esc_attr($val) . '">' . esc_html($val) . '</option>';
        }
    }

echo '</select>';
?>

Notice the strict comparison (===) between the loop variable and the retrieved $_GET parameter. This ensures that only the matching option receives the selected attribute. It is also critical to use esc_attr() for values inside attributes and esc_html() for content within tags to prevent cross-site scripting (XSS) vulnerabilities, a best practice especially important when dealing with user-controlled form data.

Handling Default States

Consider the edge case where a user submits the form but selects an option that is not in the database or was deleted before the redirect occurred. In this situation, $_GET['name'] might return an empty string or a value that does not match any available options. The code above handles this gracefully because the condition $value === $name will simply never evaluate to true, leaving all options unselected.

However, if your application logic dictates that a field must have a value (e.g., a required dropdown), you should provide a fallback default. You can modify the retrieval line to supply a default:

$name = isset($_GET['name']) ? $_GET['name'] : 'a'; // Default to first option if missing

This ensures the form never appears broken, though it is often better UX to show an empty dropdown rather than forcing a specific value silently.

Framework Considerations

If you are using a modern PHP framework like Laravel instead of raw WordPress templates, the implementation shifts slightly to leverage built-in helpers that handle escaping and repetition automatically. For instance, if you are building a resource controller, you would typically bind the form field directly to the request input or a model attribute. Laravel's Blade engine provides old('name') which retrieves the value from the previous request session if it is not present in the current request(), effectively solving this persistence problem without manual if checks for every single loop iteration.

For more complex scenarios involving form validation and mass assignment, frameworks like Laravel simplify the process by automatically populating the view with validated input data. If you are working on a larger project where maintaining custom rendering logic becomes cumbersome, exploring how to integrate robust form handling libraries or leveraging the framework's native capabilities can streamline maintenance. Resources such as https://laravelcompany.com offer further insights into building scalable PHP applications where these patterns are standard practice.

The core principle remains unchanged regardless of the environment: you must explicitly tell the browser which option is selected by modifying the DOM before sending the response.

Our network