Today, We want to share with you PHP Multiple Checkbox Array.In this post we will show you how to insert multiple selected checkbox values in database in php, hear for Get Multiples Values of Selected Checkboxes in PHP we will give you demo and example for implement.In this post, we will learn about how to get checkbox value in php if checked with an example.
PHP Multiples Checkbox Array
Create Form with Multiples Checkboxes
<form action="" method="post"> <label> Rahul <input type="checkbox" name="memberInfo[]" value="Rahul"> </label> <label> Virat <input type="checkbox" name="memberInfo[]" value="Virat"> </label> <label> Mayur <input type="checkbox" name="memberInfo[]" value="Mayur"> </label> <label> bhavik <input type="checkbox" name="memberInfo[]" value="bhavik"> </label> <input type="submit" name="submit" value="Choose options" /> </form>
Read Multiples Values from Selected Checkboxes
Use the foreach() loop to iterate
Use the foreach() loop to iterate over every selected value of checkboxes and print on the member screen.
<?php if(isset($_POST['submit'])){ if(!empty($_POST['memberInfo'])){ foreach($_POST['memberInfo'] as $checked){ echo $checked."</br>"; } } } ?>
Checkboxes Validation in PHP
<?php if(isset($_POST['submit'])){ if(!empty($_POST['memberInfo'])){ foreach($_POST['memberInfo'] as $checked){ echo $checked . '<br>'; } } else { echo '<div class="error">Checkbox is not selected!</div>'; } } ?>
Checkboxes with Custom Style
<form action="" method="post"> <label> Rahul <input type="checkbox" name="memberInfo[]" value="Rahul"> <span></span> </label> <label> Virat <input type="checkbox" name="memberInfo[]" value="Virat"> <span></span> </label> <label> Mayur <input type="checkbox" name="memberInfo[]" value="Mayur"> <span></span> </label> <label> bhavik <input type="checkbox" name="memberInfo[]" value="bhavik"> <span></span> </label> <input type="submit" name="submit" value="Choose options" /> </form>
CSS Code
Style checkbox with only HTML and CSS.
label { display: block; position: relative; padding-left: 35px; margin-bottom: 20px; cursor: pointer; font-size: 22px; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; line-height: 25px; } /* Hide the browser's default checkbox */ label input { position: absolute; opacity: 0; cursor: pointer; height: 0; width: 0; } /* Create a custom checkbox */ span { position: absolute; top: 0; left: 0; height: 25px; width: 25px; background-color: #cccccc; } label:hover input~span { background-color: #cccccc; } label input:checked~span { background-color: #1A33FF; } span:after { content: ""; position: absolute; display: none; } label input:checked~span:after { display: block; } label span:after { left: 9px; top: 5px; width: 5px; height: 10px; border: solid white; border-width: 0 3px 3px 0; -webkit-transform: rotate(45deg); -ms-transform: rotate(45deg); transform: rotate(45deg); }
PHP Checkboxes Code Example
Here is the Full source code example for checkboxes in PHP.
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <title>PHP - Get Multiple Checkbox Value DEMO</title> </head> <body> <div class="container mt-5"> <form action="" method="post" class="mb-3"> <label> Rahul <input type="checkbox" name="memberInfo[]" value="Rahul"> <span></span> </label> <label> Virat <input type="checkbox" name="memberInfo[]" value="Virat"> <span></span> </label> <label> Mayur <input type="checkbox" name="memberInfo[]" value="Mayur"> <span></span> </label> <label> bhavik <input type="checkbox" name="memberInfo[]" value="bhavik"> <span></span> </label> <input type="submit" name="submit" value="Choose options" /> </form> <?php if(isset($_POST['submit'])){ if(!empty($_POST['memberInfo'])){ foreach($_POST['memberInfo'] as $checked){ echo $checked . '<br>'; } } else { echo '<div class="error">Checkbox is not selected!</div>'; } } ?> </div> </body> </html>
get multiple checkbox value in php
To pass the multiple checkbox values in one POST action in PHP you can refer below code:
Get Multiple Values of Selected Checkboxes in PHP
To get multiple values of selected checkboxes in PHP, you can use the $_POST superglobal variable, which is an associative array that contains the values of all the form elements submitted via the HTTP POST method. Here’s an example code snippet that demonstrates how to get the values of multiple checkboxes that have the same name:
HTML form:
<form method="post" action="process.php"> <input type="checkbox" name="colors[]" value="red"> Red<br> <input type="checkbox" name="colors[]" value="green"> Green<br> <input type="checkbox" name="colors[]" value="blue"> Blue<br> <input type="submit" name="submit" value="Submit"> </form>
PHP code to process the form data:
if(isset($_POST['submit'])){ if(!empty($_POST['colors'])){ foreach($_POST['colors'] as $color){ echo $color . "<br>"; } } else{ echo "No colors selected"; } }
In this example, the colors[] name attribute is used to create an array of checkbox values. When the form is submitted, the PHP code checks if the colors[] array is not empty, and if it’s not empty, it loops through the array using a foreach loop to display each selected color value. If no colors are selected, the code displays a message saying “No colors selected”.
I hope you get an idea about how to use checkbox 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.