Login | Contact Us | Site Map | Resources

PHP Form Handling & Browser Interaction

Home > PHP Tutorial > Getting & Posting Checkbox & Radio Button Results

Getting & Posting Checkbox & Radio Button Results

Obtaining data from radio buttons and checkboxes can be a little tricky, generally due to a lack of understanding of how radio buttons and checkboxes work.

It is important to remember two facts:

Checkbox "Names" Must Be Unique to Each Checkbox
Radio Button "Names" Must Be Identical For Each Group of Buttons

Let's jump right into an example:

<form action="php-forms-get-post-checkbox-radio-data.php" method="post">

  Why don't they play poker in the jungle?<br>
  <input type="radio" name="jungle" value="treefrog"> Too many tree frogs.<br>
  <input type="radio" name="jungle" value="cheetah"> Too many cheetahs.<br>
  <input type="radio" name="jungle" value="river"> Too many rivers.<br><br>

  Check the box if you want your answer to be graded:
  <input type="checkbox" name="grade" value="yes"><br><br>

  <input type="submit" name="submit" value="Submit"><br>

</form>

The code produces the following result:

Why don't they play poker in the jungle?
Too many tree frogs.
Too many cheetahs.
Too many rivers.

Check the box if you want your answer to be graded:


Now let's use the $_POST[] superglobal to get the form data that was submitted. It is best to begin by checking to see if the form was submitted. This can be done using the isset() function to see if the submit button has been clicked.

<?php
  if (isset($_POST['submit'])) { /* Do Something Here */ }
  else { echo "Please submit the form."; }
?>

As you can see, the name of each $_POST[] superglobal corresponds with the name of the HTML form field in question.

Next, we can check to see if the submitted form should be graded. Checkboxes only return a value if they are checked. If a checkbox is not checked, then no value will be sent, so we can use the empty() function to determine our next course of action.

<?php
  if (isset($_POST['submit'])) {
    if (!empty($_POST['grade'])) { /* Grade the question. */ }
    else { echo "Your answer will not be graded."; }
  } else { echo "Please submit the form."; }
?>

Now we can find out which answer was chosen.

<?php
  if (isset($_POST['submit'])) {
    if (!empty($_POST['grade'])) {
      if (!empty($_POST['jungle'])) { /* Get Radio Button Value */ }
      else { echo "You did not choose an answer."; }
    } else { echo "Your answer will not be graded."; }
  } else { echo "Please submit the form."; }
?>

And at last, we can determine whether or not it was the right answer.

<?php
  if (isset($_POST['submit'])) {
    if (!empty($_POST['grade'])) {
      if (!empty($_POST['jungle'])) {
        if ($_POST['jungle']=="cheetah") { echo "You got the right answer!"; }
        else { echo "Sorry, wrong answer."; }
      } else { echo "You did not choose an answer."; }
    } else { echo "Your answer will not be graded."; }
  } else { echo "Please submit the form."; }
?>

Submit the form in the example at the top of the page to see the results below. (Notice that the form action will return you to this page when the form is submitted.)

Please submit the form.