Checking set values (radio buttons) in PHP

This tutorial explains how to use radio buttons in forms and check set values in PHP to ensure that the user has entered data in the form. Watch the video below and then scroll down to see the sample code.

Sample PHP code:

Step 1: Create a PHP file (eg. form.php) which contains a form with radio buttons by copying the following code. This example will quiz the user on the capital city of Australia.

  <title>Form that checks set values</title>
  <form action="action_handler.php" method="POST">
    <p>What is the capital city of Australia?</p>
    <input type="radio" name="capital" value="Sydney"> Sydney<br>
    <input type="radio" name="capital" value="Canberra"> Canberra<br>
    <input type="radio" name="capital" value="Melbourne"> Melbourne
    <p><input type="submit"></p>

Step 2: Create a separate PHP file called action_handler.php and copy the following code into it. This file will handle the form data.

    $capital = $_POST['capital'];
    $capital = NULL;

  if($capital != NULL){
    if($capital != "Canberra"){
      echo "$capital is incorrect";
      echo "$capital is correct";
    echo "You must select an answer";