How To Read If A Checkbox Is Checked In PHP? - GeeksforGeeks

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= "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> <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:

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>GeeksforGeeks 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"> <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> <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:

  • http://php.net/manual/en/function.isset.php
  • http://php.net/manual/en/function.empty.php
author sarthak_ishu11 Follow Improve Next Article How to check a key exists in an array in PHP ?

Từ khóa » Html Checkbox Php If Checked