How do I get the value of a radio button in PHP?

How do I get the value of a radio button in PHP?

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

Stefan Bogdanescu

Founder & Senior Architect

The Hidden Barrier Between Form Submission and PHP Processing

Your Result.php script fails to output anything because the data never reaches it in the first place. You are attempting to read from a source that the browser has effectively blocked or ignored due to a fundamental mismatch in how your HTML forms are structured versus how you are trying to access the submitted data.

The root cause lies in the separation of your form elements and your action attribute. In your Index.html, you have defined two distinct <form> tags. The first form handles the radio buttons, but it lacks an explicit action attribute and a method that directs the data anywhere other than the default behavior (which is often irrelevant when using multiple forms). Crucially, the second form contains the submit button, but this form has no inputs at all. When you click "Result", the browser initiates a POST request to Result.php based on the second form's configuration.

However, the second form does not contain the <input type="radio"> elements. It only contains the submit button. Because these inputs are nested inside the first <form> tag, they are completely disconnected from the event triggered by the button in the second form. When the browser processes the click on "Result", it serializes the data found within that specific form scope. Since the radio buttons reside outside the scope of the active submission request, $_POST["MyRadio"] returns an empty string or is undefined, causing your PHP logic to fall through without executing any of your echo statements.

Unifying the Form Scope

To resolve this, you must consolidate your inputs and your action into a single form structure. The submit button needs to be physically located inside the same <form> tag as the radio buttons. This ensures that when the user clicks "Result", the browser gathers all visible controls within that specific container and sends them to the target URL.

Here is how the corrected HTML should look:

<form method="POST" action="Result.php">
    <input type="radio" name="MyRadio" value="First" checked>First<br>
    <input type="radio" name="MyRadio" value="Second">Second
    <button type="submit">Result</button>
</form>

By moving the submit button into the first form and explicitly setting action="Result.php" on that same tag, you ensure that the POST request includes the MyRadio variable. Your PHP script at Result.php will then correctly populate $radioVal.

Handling Empty Values and Safety Checks

Even after fixing the HTML structure, there is a critical defensive programming practice you should adopt. In PHP, accessing an array index via $_POST["MyRadio"] when no value exists returns an empty string ("") rather than null. While your current logic checks for "First" and "Second", it does not explicitly handle the case where the variable is empty.

If a user submits the form without selecting anything (which is impossible with radio buttons due to browser constraints, but theoretically possible if you ever switch to checkboxes or dropdowns), or if you refactor this code later, relying solely on equality checks can be risky. It is best practice to check for the existence of the key first using isset() or verifying that the value is not empty.

<?php
// Safeguard against missing keys
if (isset($_POST["MyRadio"])) {
    $radioVal = $_POST["MyRadio"];

    if ($radioVal === "First") {
        echo("You chose the first button. Good choice. :D");
    } elseif ($radioVal === "Second") {
        echo("Second, eh?");
    } else {
        // This block catches unexpected states or empty strings
        echo("No selection found.");
    }
} else {
    echo("Form submission detected but no data received.");
}
?>

Using the strict comparison operator (===) is also recommended here. It ensures that type juggling does not cause unexpected matches, such as comparing a string to an integer representation of zero.

Scaling with Modern Frameworks

While fixing your vanilla PHP implementation addresses the immediate issue, consider the long-term maintenance costs of managing raw POST arrays. As applications grow, handling form data manually becomes error-prone and tedious. You must constantly remember to check for isset(), sanitize inputs, and validate types.

This is precisely where frameworks like Laravel simplify development. Laravel provides robust helpers like request() and validation pipes that abstract away these low-level concerns. For instance, instead of manually checking $radioVal, you could use request('MyRadio') which returns null if the input doesn't exist, making your conditional logic cleaner and less prone to notices.

If you are looking to migrate away from raw PHP or need a more robust architecture for handling form submissions and user sessions, exploring modern solutions like https://laravelcompany.com can provide insights into how these frameworks handle request management out of the box. They often include built-in validation rules that ensure MyRadio is actually set before your business logic even runs, preventing silent failures.

The lesson remains clear: HTML forms define a specific scope of data collection. If your inputs and your submit button are separated by different form tags, they cannot communicate, no matter how correct your PHP logic appears to be.

Our network