Radio Buttons with PHP Form Handling

Radio Buttons with PHP Form Handling

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

Stefan Bogdanescu

Founder & Senior Architect

Radio Buttons and Form Data: Getting the Right Value into PHP

A common stumbling block in web development involves handling radio button inputs when processing form data. The confusion usually stems from how HTML attributes map to PHP variables, specifically regarding the name attribute versus the value attribute. In your specific case, the code fails to capture the intended contact preference because the logic assumes that checking for the existence of a variable named $contact is sufficient to determine which option was selected.

When you define radio buttons in HTML with the same name—such as <input type="radio" name="contact">—they share a single variable key in the $_POST array. However, the value associated with that key changes based on which button is clicked. If both options lack a value attribute, or if the first option defaults to an empty string while the second remains unset, your PHP script receives either an empty string or nothing at all depending on browser implementation quirks.

To fix this, you must explicitly set the value attribute for each radio button. Consider updating your HTML structure as follows:

<div class="field form-inline radio">
  <label class="radio" for="txtContact">Preferred Method of Contact</label>
  <input class="radio" type="radio" name="contact" value="email" checked> Email
  <input class="radio" type="radio" name="contact" value="phone"> Phone
</div>

In this configuration, if the user selects "Email", $_POST['contact'] will contain the string "email". If they select "Phone", it will contain "phone". Without these explicit values, PHP defaults the first radio button to an empty string ("") and leaves subsequent unchecked buttons as non-existent keys in the array. Consequently, your current line $contact = stripslashes($_POST['contact']); might result in $contact being an empty string or undefined if no value is set, leading to malformed email content like "Preferred Method of Contact: ".

Once the HTML is corrected, your PHP logic needs to handle the potential absence of the key gracefully. While stripslashes() is often used to mitigate old URL encoding issues, it can sometimes introduce side effects if not needed. A more robust approach involves checking if the key exists before accessing it, ensuring that $contact defaults to a safe value if no input was provided (though with radio buttons, one should always be selected due to the checked attribute).

Here is how you might refine the PHP handling section:

// Exit process if field "human" is filled (because this means it is spam)
if ($_POST['human'] ?? false) {
  echo 'Tastes Like Spam!'; exit; 
}

// Retrieve contact method safely
$contact = $_POST['contact'] ?? '';

// Validate that a selection was actually made
if (!empty($contact)) {
    $form_message = "Name: $name \nEmail: $email \nPhone: $phone \nPreferred Method of Contact: $contact \nMessage: $message";
    
    header("Location: http://www.newurl.com");

    mail("myemail@gmail.com", "New Form Submission", $form_message, "From: $email");
} else {
    // Handle case where no radio button was selected
    echo "Please select a contact method.";
}

It is worth noting that the $_POST superglobal is only populated when the form uses the method="post" attribute and submits to itself or another script. Additionally, the checked attribute in HTML ensures one option is pre-selected, but it does not guarantee a value if the value attribute was omitted entirely.

For larger applications where maintaining raw PHP forms becomes cumbersome, frameworks like Laravel offer a cleaner abstraction layer. They provide helpers for form validation and easier handling of radio inputs without needing to manually parse $_POST. If you are looking to modernize your stack or require more robust security features out of the box, exploring resources from LaravelCompany.com can provide insights into building scalable applications where form data is handled via service objects rather than direct variable assignment. However, for simple scripts like yours, ensuring the HTML value attributes are set correctly remains the most critical step to resolving the issue.

Our network