Sunday, December 29, 2024
Google search engine
HomeLanguagesHow to read if a checkbox is checked in PHP?

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:




<?php 
  
if(isset($_GET['submit'])) {
    $var = $_GET['option1'];
    if(isset($var)) {
        echo "Option 1 submitted successfully";
    }
}
?>
<html lang="en">
<head>
    <title>neveropen 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">neveropen</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>


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:




<?php
if(!empty($_GET['submit'])) {
    $var = $_GET['option1'];
    if(isset($var)){
        echo "Option 1 submitted successfully";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>neveropen 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">neveropen</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>


Output:

Reference:

RELATED ARTICLES

Most Popular

Recent Comments