Current Location: Home> Latest Articles> PHP Form Handling Tips: Multi-Choice Boxes, Radio Buttons, and Dropdown List Usage Explained

PHP Form Handling Tips: Multi-Choice Boxes, Radio Buttons, and Dropdown List Usage Explained

M66 2025-06-20

PHP Form Handling Tips: Multi-Choice Boxes, Radio Buttons, and Dropdown List Usage Explained

In web development, forms are one of the most important ways users interact with a website. Multi-choice boxes, radio buttons, and dropdown lists are common input elements within forms. This article provides a detailed guide on how to handle these form elements using PHP, including relevant code examples to help developers master these common tasks.

1. Multi-Choice Boxes

Multi-choice boxes allow users to select multiple options. In HTML, you can create a multi-choice box using the tag. When the form is submitted, PHP can retrieve the selected values through the $_POST or $_GET global variables.

Code example:

<form method="post" action="submit.php">
  <input type="checkbox" name="color[]" value="red">Red
  <input type="checkbox" name="color[]" value="blue">Blue
  <input type="checkbox" name="color[]" value="green">Green
  <input type="submit" value="Submit">
</form>
<?php
if(isset($_POST['color'])){
    $selectedColors = $_POST['color'];
    foreach($selectedColors as $color){
        echo $color . "<br>";
    }
}
?>

In this example, the name attribute of the multi-choice boxes is set to color[], which allows PHP to receive the selected values as an array. We then loop through the array to output each selected color.

2. Radio Buttons

Radio buttons allow users to select one option from a group of choices. In HTML, you can create radio buttons using the tag. Similar to multi-choice boxes, when the form is submitted, PHP can retrieve the selected value through the $_POST or $_GET global variables.

Code example:

<form method="post" action="submit.php">
  <input type="radio" name="gender" value="male">Male
  <input type="radio" name="gender" value="female">Female
  <input type="submit" value="Submit">
</form>
<?php
if(isset($_POST['gender'])){
    $selectedGender = $_POST['gender'];
    echo "You selected gender: " . $selectedGender;
}
?>

In this example, the name attribute of the radio buttons is set to gender, and PHP will retrieve the selected value using $_POST['gender'].

3. Dropdown List

A dropdown list allows users to choose from a list of predefined options. In HTML, you can create a dropdown list using the