Skip to content
PHP Logo

What are some recommended resources for learning the basics of handling radio buttons in PHP?

Radio buttons in PHP are used to allow users to select only one option from a group of options. To handle radio buttons in PHP, you can use the $_POST superglobal to retrieve the selected value from the form submission. You can then use conditional statements to process the selected option accordingly.

php
<form method="post">
  <input type="radio" name="color" value="red"> Red
  <input type="radio" name="color" value="blue"> Blue
  <input type="radio" name="color" value="green"> Green
  <input type="submit" name="submit" value="Submit">
</form>

<?php
if(isset($_POST['submit'])){
  $selected_color = $_POST['color'];
  
  switch($selected_color){
    case 'red':
      echo "You selected Red";
      break;
    case 'blue':
      echo "You selected Blue";
      break;
    case 'green':
      echo "You selected Green";
      break;
    default:
      echo "Please select a color";
  }
}
?>