receiving radio box value in php

receiving radio box value in php

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

Stefan Bogdanescu

Founder & Senior Architect

Receiving Radio Box Values in PHP

Radio buttons present a specific challenge when handling form data because they exist as mutually exclusive options within a single field. When a user submits a form containing inputs like <input type="radio" name="radio" value="yes"> and <input type="radio" name="radio" value="no">, the underlying mechanism relies on the name attribute to group them. Only one input in that specific group can be active at any given time.

To retrieve this value in PHP after a form submission, you must check if the field exists in the incoming data array. Using the superglobal $POST is standard practice for HTML forms submitted via the POST method. However, you should also account for cases where no option is selected or the form is accessed via GET.

A robust approach involves using the isset() function to prevent undefined index notices. If you simply access $_POST['radio'] without checking existence first, and the user has not selected an option (or the browser defaults to not sending empty radio values), PHP will throw a warning. The safest pattern looks like this:

if (isset($_POST['radio'])) {
    $selectedValue = $_POST['radio'];
} else {
    // Handle the case where nothing was selected
    // Perhaps set a default or initialize as null
    $selectedValue = null; 
}

It is also crucial to consider browser behavior regarding empty radio buttons. If your HTML lacks the required attribute on individual radios, some browsers might submit an empty string if no button is clicked, while others might not include the key in the POST array at all depending on how the form was submitted. Checking with isset() ensures your application remains stable regardless of the browser's specific implementation details.

Persisting Selections on Page Reload

Once a value is received, you often need to remember that selection when the page reloads, typically after processing the form data and displaying a result. This is essential for user experience; if a user submits a preference and sees a confirmation message, they should not have to re-select their original choice immediately.

To achieve this, you need to persist the selected value in your application's state before rendering the HTML view again. The method depends on your architecture:

  1. Server-Side Sessions: If you are using a session-based approach (common in PHP), store the value in $_SESSION['radio_selection'] upon form submission. When generating the HTML for the same page, iterate through your radio options and add the checked attribute to the input that matches the stored session value.
  2. Database Persistence: If the form represents a permanent state change, save the selection to your database. Then, when rendering the view, query the database to determine which radio button corresponds to the current record's value.

Here is how you might construct the HTML dynamically in PHP based on a stored value:

$storedValue = $_SESSION['radio_selection'] ?? null;

// Iterate over your options
$options = ['yes', 'no'];

foreach ($options as $value) {
    // Add the checked attribute if the current value matches the stored one
    echo '<input type="radio" name="radio" value="' . $value . '"';
    
    if ($storedValue === $value) {
        echo ' checked="checked"';
    }
    
    echo '> ' . ucwords($value) . '</input>';
}

This logic ensures that the visual state of the form reflects the actual data held in your application, preventing user confusion. A common pitfall here is forgetting to handle the initial load where no session data exists yet; always use a null coalescing operator or an isset() check when retrieving the stored value to default gracefully if nothing has been saved previously.

If you are building a larger application using Laravel, this pattern integrates seamlessly with form request validation and flash data for redirection, ensuring that user inputs are handled securely and consistently throughout the request lifecycle.

Our network