How to Read Whether a Checkbox is Checked in PHP

  1. Applying the isset() Function
  2. Applying the empty() Function

In this tutorial, you can find comprehensive information on how to read whether a checkbox is checked in PHP.

Here, we will demonstrate two handy functions that will assist you in reading whether a checkbox is checked in PHP. Those functions are isset() and empty().

Applying the isset() Function

This is an inbuilt function that is capable of checking whether a variable is set.

With the isset() function, you can also check whether an array, a declared variable, or an array key is null. The isset() function returns false if it does and true in all the possible situations.

The syntax of the isset() function looks like this:

bool isset( $var, mixed )

Now, let’s see the code that will check whether a checkbox is checked:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>W3docs Example</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <style>
      .gfg {
        font-size: 40px;
        font-weight: bold;
        color: green;
      }      
      body {
        text-align: center;
      }
    </style>
  </head>
  <body>
    <?php
      if(isset($_GET['submit'])) { 
        $var = $_GET['option1']; 
	    if(isset($var)) { 
	      echo "Option 1 submitted successfully"; 
        } 
      } 
    ?>
    <div class="container">
      <div class="gfg">W3docs</div>
      <h2>Form control: checkbox</h2>
      <p>The form below contains one checkbox.</p>
      <form method="get" action="/form/submit">
        <div class="checkbox">
          <label>
            <input type="checkbox" name="option1" value="Option 1">Option 1
          </label>
          <button name="submit" value="true">SUBMIT</button>
        </div>
      </form>
    </div>
  </body>
</html>

The output of this code will show that option 1 is successfully submitted.

Applying the empty() Function

In this section, we will illustrate another inbuilt function: the empty() function.

With empty(), you can test whether a variable is empty or not.

The syntax is as follows:

bool empty( $var )

Let’s see how to use this function for reading if a checkbox is checked:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>W3docs</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
    </script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js">
    </script>
    <style>
      .gfg {
        font-size: 40px;
        font-weight: bold;
        color: green;
      }
      body {
        text-align: center;
      }
    </style>
  </head>
  <body>
    <?php
      if(!empty($_GET['submit'])) { 
      $var = $_GET['option1']; 
      if(isset($var)){ 
        echo "Option 1 submitted successfully"; 
      } 
     } 
    ?>
    <div class="container">
      <div class="gfg">W3docs</div>
      <h2>Form control: checkbox</h2>
      <p>The form below contains one checkbox.</p>
      <form method="get" action="/form/submit">
        <div class="checkbox">
          <label>
            <input type="checkbox" name="option1" value="Option 1">Option 1
          </label>
          <button name="submit" value="true">SUBMIT</button>
        </div>
      </form>
    </div>
  </body>
</html>

The output will show that option 1 is successfully submitted.


Do you find this helpful?

Related articles