Get multiple checkbox value using jquery PHP

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

Get multiple checkbox value using jquery PHP

There are the Following The simple About PHP: Get Values of Multiple Checked Checkboxes Full Information With Example and source code.

  Laravel Custom Namespaces Example Tutorial

As I will cover this Post with live Working example to develop how to get checked and unchecked checkbox array value in php, so the How to get the values of selected checkboxes in a group using jQuery is used for this example is following below.

Step 1. Read $_POST checked values

HTML Script

<span>Select playerss</span><br/>
<input type="checkbox" name='player[]' value="PHP"> PHP <br/>
<input type="checkbox" name='player[]' value="JavaScript"> JavaScript <br/>
<input type="checkbox" name='player[]' value="jQuery"> jQuery <br/>
<input type="checkbox" name='player[]' value="Angular JS"> Angular JS <br/>

PHP script

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

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

}

full Source code

<form method="post" action="">
    <span>Select playerss</span><br/>
    <input type="checkbox" name='player[]' value="Virat"> Virat <br/>
    <input type="checkbox" name='player[]' value="Rohit"> Rohit <br/>
    <input type="checkbox" name='player[]' value="Dhavan"> Dhavan <br/>
    <input type="checkbox" name='player[]' value="Angular JS"> Angular JS <br/>

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

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

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

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

    }

}
?>

Step 2. Create Table structure

playerss table

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

Step 3. Configuration

Create a new config.php file.

<?php

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

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

Step 4. Insert and Display checked values from Database

Completed PHP Source Code

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

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

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

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

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

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

    $checked_arr = array();

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

    // Create checkboxes
    $playerss_arr = array("PHP","JavaScript","jQuery","AngularJS");
    foreach($playerss_arr as $players){

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

  </body>
</html>

Web Programming Tutorials Example with Demo

Read :

  PHP Multiple Checkbox Array Handling Tutorial Example

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about jquery value of input checkbox.
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.

Leave a Reply

avatar
  Subscribe  
Notify of