Multiple checkbox in php Example with Demo

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:

Read Also:  how to use checkbox in php?

//Html form source code
<form method="post" action="send_data.php">
    <input type="checkbox" name="membercode[]" value="member1">Member1
    <input type="checkbox" name="membercode[]" value="member2">Member2
    <input type="checkbox" name="membercode[]" value="member3">Member3
    <input type="checkbox" name="membercode[]" value="member4">Member4
    <input type="submit" name="submit_data"/>
</form>

send_data.php

<?php
if(isset($_POST["submit_data"])){
   if(!empty($_POST["membercode"])){
       // to check the membercode checkboxes values you can use loop to display each checkbox value
       $membercodes = $_POST["membercode"];
       foreach($membercodes as $member){
            echo "Member code :".$member;
       }
   } 
}
?>

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.