how to get checkbox value in php if checked?

Today, We want to share with you how to get checkbox value in php if checked.In this post we will show you how to get checked and unchecked checkbox value in php, hear for The checkbox element in HTML allows us to select multiple items from the group of values. we will give you demo and example for implement.In this post, we will learn about Get multiple checkbox value using jquery PHP with an example.

Get checked Checkboxes value with PHP

Laravel get multiple checkbox value From View to Controller
Laravel get multiple checkbox value From View to Controller

1. Read $_POST checked values
<span>Select players</span><br/>
<input type="checkbox" name='player_name[]' value="PHP"> PHP <br/>
<input type="checkbox" name='player_name[]' value="JavaScript"> JavaScript <br/>
<input type="checkbox" name='player_name[]' value="jQuery"> jQuery <br/>
<input type="checkbox" name='player_name[]' value="Angular JS"> Angular JS <br/>

PHP

if(isset($_POST['submit'])){

    if(!empty($_POST['player_name'])) {    
        foreach($_POST['player_name'] as $value){
            echo "value : ".$value.'<br/>';
        }
    }

}

Completed Code

<form method="post" action="">
    <span>Select players</span><br/>
    <input type="checkbox" name='player_name[]' value="parthive"> Parthiv <br/>
    <input type="checkbox" name='player_name[]' value="jasmit"> Jasmit <br/>
    <input type="checkbox" name='player_name[]' value="jadeja"> Jadeja <br/>
    <input type="checkbox" name='player_name[]' value="ajankye Rahane"> Ajankye <br/>

    <input type="submit" value="Submit" name="submit">
</form>

<?php
if(isset($_POST['submit'])){

    if(!empty($_POST['player_name'])) {

        foreach($_POST['player_name'] as $value){
            echo "value : ".$value.'<br/>';
        }

    }

}
?>

3. Table structure

CREATE TABLE `players` (
  `id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `player` varchar(80) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Configuration

config.php

<?php

$host = "localhost"; 
$user = "root"; 
$password = ""; 
$dbname = "players_list"; 

$con = mysqli_connect($host, $user, $password,$dbname);
// Check connection
if (!$con) {
  die("Connection failed: " . mysqli_connect_error());
}

Insert and Display checked values from Database

Completed Code

<?php
include "config.php";
?>
<!doctype html>
<html>
  <head>

  <?php
  if(isset($_POST['submit'])){

    if(!empty($_POST['player_name'])) {

      $player_name = implode(",",$_POST['player_name']);

      // Insert and Update record
      $checkEntries = mysqli_query($con,"SELECT * FROM players");
      if(mysqli_num_rows($checkEntries) == 0){
        mysqli_query($con,"INSERT INTO players(player) VALUES('".$player_name."')");
      }else{
        mysqli_query($con,"UPDATE players SET player='".$player_name."' ");
      }
 
    }

  }
  ?>
  </head>
  <body>
  <form method="post" action="">
    <span>Select players</span><br/>
    <?php

    $checked_arr = array();

    // Fetch checked values
    $fetchLang = mysqli_query($con,"SELECT * FROM players");
    if(mysqli_num_rows($fetchLang) > 0){
      $result = mysqli_fetch_assoc($fetchLang);
      $checked_arr = explode(",",$result['player']);
    }

    // Create checkboxes
    $players_arr = array("Parthiv","Jasmit","Jadeja","Ajankye");
    foreach($players_arr as $player){

      $checked = "";
      if(in_array($player,$checked_arr)){
        $checked = "checked";
      }
      echo '<input type="checkbox" name="player_name[]" value="'.$player.'" '.$checked.' > '.$player.' <br/>';
    }
    ?>
 
    <input type="submit" value="Submit" name="submit">
  </form>

  </body>
</html>

I hope you get an idea about if checkbox is checked value=1 else 0 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.