PHP check if checkbox is unchecked

You can use the jQuery prop() method to check or uncheck a checkbox dynamically such as on click of button or an hyperlink etc. The prop() method require jQuery 1.6 and above.

Let's try out the following example to understand how it basically works:





jQuery Check/Uncheck Checkbox




    

Are you sure?

Yes No


Here are some more FAQ related to this topic:

We will demonstrate how to check whether the checkbox is checked in PHP using the isset() function on $_POST array. We provide the value of the name attribute of the

#php 7.x
";
}
if(isset($_POST['test2'])){
    echo "checked value2";
}
?>
0 tag of HTML as the array element in the $_POST array.

We will introduce another method to read the checkbox if it is checked in PHP using the

#php 7.x
";
}
if(isset($_POST['test2'])){
    echo "checked value2";
}
?>
2 function. We use checkboxes as an array in this method. It means that the all name field in HTML
#php 7.x
";
}
if(isset($_POST['test2'])){
    echo "checked value2";
}
?>
0 tag must contain the same array.

We will introduce a short-hand method to check if the checkbox is checked using the ternary operator. The method is more straightforward and shorter and uses the isset() function.

Use the isset() Function on $_POST Array to Read if Checkbox Is Checked

We can use the isset() function to check whether the checkbox is checked in PHP. The isset() function takes the $_POST array as argument. The $_POST array contains the specific value of the name attribute present in HTML form.

For example, create a form in HTML with

checked value1
checked value2
3 method and specify the action to
checked value1
checked value2
4. Create two checkboxes with names
checked value1
checked value2
5 and
checked value1
checked value2
6, respectively. Save the file with
checked value1
checked value2
7 extension. Create a PHP file named
checked value1
checked value2
4. Apply two
checked value1
checked value2
9 conditions to isset() function with $_POST array as argument. Use
checked value1
checked value2
5 and
checked value1
checked value2
6 as the array elements in the $_POST arrays, respectively. Print the message specifying the respective value has been checked.

The example below uses the

checked value1
checked value2
3 method to send the data in the form. It is secure while sending sensitive information through the form. Click here to know more about the
checked value1
checked value2
3 method. The user checks both the checkbox in the form. Thus, the script outputs the way it is shown below. If the user had checked only the
#html 5

 Option 1
 Option 2
 
7, the script would output as
#html 5

 Option 1
 Option 2
 
8. It goes similar to
#html 5

 Option 1
 Option 2
 
9 too.

Example Code:

# html 5

 Option 1
 Option 2
 

#php 7.x
";
}
if(isset($_POST['test2'])){
    echo "checked value2";
}
?>

Output:

checked value1
checked value2

Use the #php 7.x "; } if(isset($_POST['test2'])){ echo "checked value2"; } ?> 2 Function to Read if the Checkbox Is Checked for Checkboxes as an Array

We can use the

#php 7.x
";
}
if(isset($_POST['test2'])){
    echo "checked value2";
}
?>
2 function to check whether an element lies within an array in PHP. The
#php 7.x
";
}
if(isset($_POST['test2'])){
    echo "checked value2";
}
?>
2 function takes the value to be checked as the first argument. The second argument of the function is the array where the value is to be checked. Check the PHP manual to know more about the
#php 7.x

3 function. For this method to work, all the name attribute values in HTML form must be an array.

For example, assign the value of name attribute in HTML form with

#php 7.x

6 array. Note it applies to all the checkbox
#php 7.x

7. First, in the PHP file, check whether the data has been submitted using the isset() function as done in the first method. But, do not use the
#php 7.x

9 brackets after the
#html 5

 Option 1
 Option 2
 

0 while checking the posted data. Then, use
#php 7.x
";
}
if(isset($_POST['test2'])){
    echo "checked value2";
}
?>
2 function to check whether the
#html 5

 Option 1
 Option 2
 

2 is in the
#html 5

 Option 1
 Option 2
 

3 array. Display the message.

At first, the example below checks whether the data is submitted in the form. If the condition is true, then it checks if

#html 5

 Option 1
 Option 2
 

2 lies in the
#html 5

 Option 1
 Option 2
 

3 array using the
#php 7.x
";
}
if(isset($_POST['test2'])){
    echo "checked value2";
}
?>
2 function. The user checks the first checkbox in the form.

Example Code:

#html 5

 Option 1
 Option 2
 

#php 7.x

Output:

Use the isset() Function With Ternary Function to Read if the Checkbox Is Checked

We can use a short-hand method to check if the checkbox has been checked in PHP. This method uses a ternary operator along with the isset() function. Please check the MSDN Web Docs to know about the ternary operator.

For example, set a variable

#html 5

 Option 1
 Option 2
 

9 to store the value of the ternary operation. Use the isset() function to check whether
checked value1
checked value2
5 has been checked in the checkbox. Print
#html 5

 Option 1
 Option 2
 

9 variable to show the result. In the example below,
#php 7.x

3 is displayed if the condition is true, and the
#php 7.x

4 is displayed if the condition is false. The user checks the second checkbox in the form. Therefore, the condition fails.

How to check if checkbox is unchecked in PHP?

When the checkbox is checked the value of the <input type=“checkbox” … /> is send with the post data, and if it isn't checked the value of the <input type=“hidden” … /> is send with the post data, so you can just check whether the value is 0 (unchecked) or 1 (checked).

How to display checked checkbox value in PHP without submit?

php: <input type='checkbox' name='checkme' value='Yes' onclick="postValue(this. value)"/> function postValue(var para){ //post the para to php script by ajax that you must master. } Save this answer.

How to show checked checkbox in PHP?

Get checked Checkboxes value with PHP.
Read $_POST checked values. HTML. ... .
Demo. View Demo..
Table structure. I am using languages table in the example. ... .
Configuration. Create a new config. ... .
Insert and Display checked values from Database. Create an Array $languages_arr to store languages names. ... .
Conclusion..