how to use checkbox in php?

Today, We want to share with you how to use checkbox in php.In this post we will show you multiple checkboxs in php, hear for how to get checked and unchecked checkboxs value in php we will give you demo and example for implement.In this post, we will learn about Get multiple checkboxs value using jquery PHP with an example.

HTML <input type="checkbox"> with PHP

How to Create Checkboxes In PHP?
Checkboxes Types

  • Checkbox group 
  • Checkbox
Read Also:  Laravel 6 Global Scope Tutorial Example

Example 1: Single Checkbox in PHP

<?php

$privacyanmdpolices=false;

If ($_POST[‘terms_of_services’]) {

$privacyanmdpolices=true;
}

If($privacyanmdpolices){
Echo ‘Terms of Service checkboxs ticked!’;
}
?>
<form action=””method=”post”>
<lable>
I accept the terms of service.
<input type=”checkbox” name=”terms_of_services” value=”Y”>
<lable>
<input type=”submit” value=”Register”>
</form>
?>

Multiple Checkbox in PHP

Example 2:

<?php

$productOptions= array(
‘mobile’,
‘laptop’,
‘computer’,
‘pendrive’
);

$products = array();

If(!empty($_POST[‘products’]) && is_array($_POST[‘products’])){

Foreach($_POST[‘products’] as $products){

If(in_array($product, $productOptions)){

$products[]=$product;
}
}
}
Var_dump($products);
?>
<form action=””method=”post”>
<label>
Tomatoes
<input type=”checkbox” name=”products[]” value=”Laptops”>
<label>
<label>
Onions
<input type=”checkbox” name=”products[]” value="Computer">
</label>
<label>
Chillies
<input type=”checkbox” name=”products[]” value="Pendrives">
</label>
<input type=”submit” value=”Register”>
</form>

I hope you get an idea about how to display checked checkboxs value in php.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.