HTML Radio Button: Select One Option from a Group

HTML radio buttons are used when a user should choose one option from a mutually exclusive group. Radio buttons belong to the same group when they use the same name.

<fieldset>
  <legend>Gender</legend>

  <input type="radio" id="gender_male" name="gender" value="Male" checked>
  <label for="gender_male">Male</label>

  <input type="radio" id="gender_female" name="gender" value="Female">
  <label for="gender_female">Female</label>
</fieldset>
Gender
HTML form radio buttons
Grouping rule: radio buttons that represent one choice must share the same name. Their value attributes identify the individual options.

Basic Radio Button Syntax Top ↑

<input type="radio" name="groupname" value="value1" checked>

The checked attribute is optional. If no radio button in the group starts checked, the form initially has no selected value for that group.

Radio Button Attributes Top ↑

AttributePurpose
type="radio"Creates a radio button.
nameGroups related radio buttons and defines the submitted field name.
valueDefines the value submitted when that option is selected.
idUniquely identifies the control and connects it to a label.
checkedMakes that option selected initially.
requiredRequires a selection in the radio group for browser constraint validation.
disabledDisables the option and normally excludes it from form submission.
Modern HTML note: the old align attribute is not the way to position radio buttons. Use CSS for spacing and layout.

Grouping Radio Buttons with name Top ↑

The browser treats radio buttons with the same name as one mutually exclusive group.

<input type="radio" id="delivery_standard" name="delivery" value="standard">
<label for="delivery_standard">Standard</label>

<input type="radio" id="delivery_express" name="delivery" value="express">
<label for="delivery_express">Express</label>

Selecting one option deselects the other option in the same group.

Radio buttons with different names belong to different groups, so one option can be selected independently in each group.

value Attribute Top ↑

The value is what the browser submits for the selected radio button.

<input type="radio" name="plan" value="basic">
<input type="radio" name="plan" value="pro">

If the second option is selected, the submitted name/value pair is equivalent to:

plan=pro
Give each option in a group a meaningful value. If value is omitted, a selected radio control normally submits the default value on, which is rarely useful for distinguishing options.

checked: Default Selection Top ↑

Add the Boolean checked attribute to the option that should be selected when the page first loads.

<input type="radio" id="gender_m" name="gender" value="Male" checked>
<label for="gender_m">Male</label>

<input type="radio" id="gender_f" name="gender" value="Female">
<label for="gender_f">Female</label>

The user can normally choose another option before submitting the form.

What If No Radio Button Is Selected? Top ↑

If no option in the group is selected, that radio-group name is normally absent from the submitted data.

<input type="radio" name="contact" value="email">
<input type="radio" name="contact" value="phone">

If neither option is selected, the browser does not normally submit a contact value.

Labels, fieldset and legend Top ↑

Use a label for every radio button and use <fieldset> with <legend> to describe a related group.

<fieldset>
  <legend>Preferred contact method</legend>

  <input type="radio" id="contact_email" name="contact" value="email">
  <label for="contact_email">Email</label>

  <input type="radio" id="contact_phone" name="contact" value="phone">
  <label for="contact_phone">Phone</label>
</fieldset>

This makes the group relationship clearer visually and for assistive technologies.

Required Radio Group Top ↑

When the form must contain one selected option, use required on a radio button in the group.

<fieldset>
  <legend>Choose a plan</legend>

  <input type="radio" id="plan_basic" name="plan" value="basic" required>
  <label for="plan_basic">Basic</label>

  <input type="radio" id="plan_pro" name="plan" value="pro">
  <label for="plan_pro">Pro</label>
</fieldset>
Browser validation improves usability but can be bypassed. The server must still verify that a permitted radio value was submitted.

Disabled Radio Buttons Top ↑

<input type="radio" id="plan_enterprise" name="plan" value="enterprise" disabled>
<label for="plan_enterprise">Enterprise</label>

A disabled radio button cannot normally be selected by the user and is not included in normal form submission.

If a disabled radio button is displayed as checked, do not expect that value to be submitted.

Disabled controls are a user-interface state, not an authorization mechanism. Server-side code must enforce which options the user is actually allowed to choose.

Radio Button vs Checkbox Top ↑

Radio buttonCheckbox
Choose one option from a mutually exclusive group.Choose zero, one or several independent options.
Buttons in one group use the same name.Checkboxes may use separate names or a shared multi-value name depending on the data model.
Selecting one option deselects another option in the same group.Selecting one checkbox does not normally deselect another checkbox.

Examples:

  • Preferred payment method: radio buttons, because one option is selected from the group.
  • Programming languages known: checkboxes, because several choices may apply.

See HTML checkboxes.

Complete Form Example Top ↑

<form action="profile.php" method="post">
  <fieldset>
    <legend>Preferred contact method</legend>

    <input type="radio" id="pref_email" name="contact" value="email" required>
    <label for="pref_email">Email</label>

    <input type="radio" id="pref_phone" name="contact" value="phone">
    <label for="pref_phone">Phone</label>
  </fieldset>

  <button type="submit">Save</button>
</form>

Receiving the Selected Value in PHP Top ↑

Do not assume that a radio-group field always exists. Validate the submitted value against the options the application permits.

<?php
$allowed=[
    'email',
    'phone'
];

$contact=$_POST['contact'] ?? null;

if($contact === null || !in_array($contact, $allowed, true)){
    echo 'Select a valid contact method.';
}else{
    echo htmlspecialchars(
        $contact,
        ENT_QUOTES,
        'Windows-1252'
    );
}

See PHP form handling for processing and validating submitted values.

Video Tutorial Top ↑

HTML form radio buttons input types to collect user selected options with attributes

Common Radio Button Mistakes Top ↑

Giving every option a different name Top ↑

If the options represent one mutually exclusive choice, they must share the same name. Different names create separate groups.

Giving every option the same value Top ↑

The group shares one name, but each option should have a value that identifies what the user selected.

Omitting value and receiving "on" Top ↑

Without an explicit value, a selected radio control normally submits on. Add meaningful values.

Assuming a radio value is always submitted Top ↑

If no radio button is selected, the group name can be absent from the submitted data.

Using radio buttons for multiple simultaneous choices Top ↑

Use checkboxes when several choices can apply at the same time.

Using the obsolete align attribute Top ↑

Use CSS for spacing, alignment and layout.

Trusting browser validation alone Top ↑

Required and disabled states improve the interface, but server-side code must still validate allowed values and permissions.

Frequently Asked Questions Top ↑

Q1: How do I group radio buttons in HTML?

Give all radio buttons in the mutually exclusive group the same name attribute.

Q2: Can more than one radio button be selected in one group?

No. Selecting one radio button in a same-named group deselects the previously selected option in that group.

Q3: What does the value attribute do?

It defines the value submitted for the selected radio button.

Q4: What does checked do?

The checked Boolean attribute makes that radio option selected when the page initially loads.

Q5: What happens if no radio option is selected?

The radio-group field is normally absent from the submitted form data.

Q6: Are disabled radio buttons submitted?

No. Disabled form controls are normally excluded from form submission.

Q7: What is the difference between radio buttons and checkboxes?

Radio buttons are for one choice from a mutually exclusive group. Checkboxes allow independent selections and can support multiple choices.


Checkbox Drop-down Listbox HTML Form



plus2net.com










We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer