PHP Radio Button

Summary: in this tutorial, you’ll learn how to create a form with radio buttons and handle radio groups in PHP.

Introduction to radio buttons #

To create a radio button, you use the <input> element with the type radio. For example:

<input type="radio" name="contact" id="contact_email" value="email" />Code language: HTML, XML (xml)

A radio button doesn’t have a label. Therefore, you should always use a radio button with a <label> element like this:

<input type="radio" name="contact" id="contact_email" value="email"/>
<label for="contact_email">Email</label>Code language: HTML, XML (xml)

To associate a radio button with a <label> element, the value of the for attribute of the label needs to be the same as the value of the id of the radio button.

A radio button has two states: checked and unchecked.

When you link a label with a radio button, you can check the radio button by clicking the label or the radio button itself. Hence, the label increases the usability of the radio button because it expands the selection area.

Alternatively, you can place the radio button within a <label> element like this:

<label>
    <input type="radio" name="contact_email" value="email"> Email
</label>Code language: HTML, XML (xml)

In this case, the radio links to the label without matching the for and id attributes.

To select a radio button automatically when the page loads for the first time, you can use the checked Boolean attribute:

<input type="radio" name="contact" id="contact_email" value="email" checked />
<label for="contact_email">Email</label>Code language: HTML, XML (xml)

Define a radio group #

In practice, you often use the radio buttons in a group. A group of radio buttons is called a radio group. A radio group allows you to select only one radio button at a time.

If you select any radio button in a group, the currently-selected radio button in the same group is automatically deselected.

To define a radio group, you assign the same name to all the radio buttons in the same group.

The following example defines a radio group that consists of two radio buttons.

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

<input type="radio" name="contact" id="contact_phone" value="phone" />
<label for="contact_phone">Phone</label>Code language: HTML, XML (xml)

Handle radio buttons in PHP #

When a form has a radio button with a name, you can get the checked radio button by accessing either $_POST or $_GET array, depending on the request method.

If a radio button is not checked, the $_POST or $_GET does not contain the key of the radio button. Therefore, you need to use the isset() function to check whether a radio button is checked or not:

isset($_POST['radio_name'])Code language: PHP (php)

Alternatively, you can use the filter_has_var() function:

filter_has_var(INPUT_POST, 'radio_name')Code language: JavaScript (javascript)

The filer_has_var() returns true if it finds the radio button name in the INPUT_POST.

If a radio button is checked, you get the value of the radio button from the $_POST using the radio button name:

$_POST['radio_name']Code language: PHP (php)

PHP radio button example #

We’ll create a simple form with a radio group. If you do not select any option and submit the form, it’ll show an error message. Otherwise, it’ll show the value of the selected radio button.