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>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> |
Output:

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:
<?phpif(!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> |
Output:

Reference:
Recommended Posts:
- How to check whether a checkbox is checked in jQuery?
- HTML | DOM Input Checkbox checked Property
- CSS | :checked Selector
- HTML | checked Attribute
- jQuery | :checked Selector
- AngularJS | ng-checked Directive
- HTML | <input>checked Attribute
- HTML | DOM Input Radio checked Property
- How to style a checkbox using CSS?
- How to set checkbox size in HTML/CSS?
- jQuery | :checkbox Selector
- HTML | DOM Input Checkbox Property
- HTML | <input type="checkbox">
- HTML | DOM Input Checkbox value Property
- HTML | DOM Input Checkbox name Property
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.



