How to read if a checkbox is checked in PHP?

Using isset() Function

The isset() function is an inbuilt function in PHP which checks whether a variable is set and is not NULL. This function also checks if a declared variable, array or array key has null value, if it does, isset() returns false, it returns true in all other possible cases. This problem can be solved with the help of isset() function.

Syntax:



bool isset( $var, mixed )

Description: This function accepts more than one parameters. The first parameter of this function is $var. This parameter is used to store the value of variable.

Program:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Using empty() Function

The empty() function is an inbuilt function in PHP which is used to check whether a variable is empty or not.

Syntax:

bool empty( $var )

Description: This function determine whether a variable is empty or not.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.