PHP - Get Multiple Checkbox Data Using jQuery

Learn on how to create a Get Multiple Checkbox Data Using jQuery. PHP is a server-side scripting language designed primarily for web development. It is a lean and consistent way to access databases. The jQuery is a fast, reliable kind of cross-platform javascript library. It is designed to simplify the traditional way of coding in javascript. So let's now do the coding...

Before we get started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And this is the link for the jquery that i used in this tutorial https://jquery.com/. Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_checkbox, after that click Import then locate the database file inside the folder of the application then click ok. tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2.         $conn = new mysqli('localhost', 'root', '', 'db_check');
  3.        
  4.         if(!$conn){
  5.                 die("Error: Can't connect to database!");
  6.         }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7. <body>
  8.         <nav class="navbar navbar-default">
  9.                 <div class="container-fluid">
  10.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11.                 </div>
  12.         </nav>
  13.         <div class="col-md-3"></div>
  14.         <div class="col-md-6 well">
  15.                 <h3 class="text-primary">PHP - Get Multiple Checkbox Data Using jQuery</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <form method="POST" id="form_data">
  18.                         <div class="col-md-2"></div>
  19.                         <div class="col-md-8">
  20.                                 <div class="form-group">
  21.                                         <label>Name:</label>
  22.                                         <input type="text" name="txt_name" id="txt_name" class="form-control" />
  23.                                 </div>
  24.                                 <div style="padding:15px;">
  25.                                         <label>Skills:</label>
  26.                                         <input type="checkbox" name="skill[]" class="skill" value="Python"/>Python
  27.                                         <input type="checkbox" name="skill[]" class="skill" value="C#"/>C#
  28.                                         <input type="checkbox" name="skill[]" class="skill" value="C++"/>C++
  29.                                         <input type="checkbox" name="skill[]" class="skill" value="Java"/>Java
  30.                                         <input type="checkbox" name="skill[]" class="skill" value="PHP"/>PHP
  31.                                         <input type="checkbox" name="skill[]" class="skill" value="VB"/>VB
  32.                                 </div>
  33.                                 <div class="form-group">
  34.                                         <button type="button" id="save" class="btn btn-primary form-control">Save</button>
  35.                                 </div>
  36.                         </div>
  37.                 </form>
  38.                 <table class="table table">
  39.                         <thead>
  40.                                 <tr>
  41.                                         <th>Name</th>
  42.                                         <th>Skill</th>
  43.                                 </tr>
  44.                         </thead>
  45.                         <tbody id = "result">
  46.                         </tbody>
  47.                 </table>
  48.         </div>
  49. </body>
  50. <script src="js/jquery-3.2.1.min.js"></script>
  51. <script src="js/bootstrap.js"></script>
  52. <script src="js/script.js"></script>
  53. </html>

Creating PHP Queries

This code contains the php query of the application. This code will save your data inputs to the database server without page refresh, and display the data within the database base via ajax request. To do that just copy and write this block of codes inside the text editor, then save it as shown below. save_query.php
  1. <?php
  2.         require_once 'conn.php';
  3.        
  4.         if($_POST['txt_name']){
  5.                 $name = $_POST['txt_name'];
  6.                
  7.                 $conn->query("INSERT INTO `member` VALUES('', '$name')");
  8.                
  9.                 $query = $conn->query("SELECT * FROM `member` WHERE `name` = '$name'");
  10.                 $fetch = $query->fetch_array();
  11.                
  12.                 foreach($_POST['skill'] as $val){
  13.                         $skill = addslashes($val);
  14.                         $conn->query("INSERT INTO `skill` VALUES('', '$skill', '$fetch[mem_id]')");
  15.                 }
  16.                
  17.                 echo "Data Inserted";
  18.         }
  19. ?>
data.php
  1. <?php
  2.         require 'conn.php';
  3.        
  4.         if(ISSET($_POST['res'])){
  5.                 $query = $conn->query("SELECT member.mem_id, member.name, GROUP_CONCAT(skill.skill SEPARATOR ', ') skill FROM `member` LEFT JOIN `skill` ON member.mem_id = skill.mem_id GROUP BY member.mem_id");
  6.                 while($fetch = $query->fetch_array()){
  7.                
  8.                 echo'
  9.                         <tr>
  10.                                 <td>'.$fetch['name'].'</td>
  11.                                 <td>'.$fetch['skill'].'</td>
  12.                         </tr>';
  13.                 }
  14.         }
  15. ?>

Creating The jQuery Script

This is where the code that uses jquery script been used. This code will sent an ajax request to the php server to retrieve the data from it, and will display in the webpage. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
  1. $(document).ready(function(){  
  2.         DisplayData();
  3.        
  4.         $('#save').on('click', function(){
  5.                
  6.                 if($('#txt_name').val() == ""){
  7.                         alert("Please enter a name");
  8.                 }else{
  9.                         if($('.skill:checkbox:checked').length > 0){
  10.                                 var dataString = $("#form_data").serialize();
  11.                                 $.ajax({
  12.                                         type: 'POST',
  13.                                         url: 'save_query.php',
  14.                                         data: dataString,
  15.                                         success: function(data){
  16.                                                 alert(data);
  17.                                                 $('#form_data')[0].reset();
  18.                                                 DisplayData();
  19.                                         }
  20.                                 });
  21.                         }else{
  22.                                 alert("Please select something");
  23.                         }
  24.                 }
  25.         });
  26.        
  27.         function DisplayData(){
  28.                 $.ajax({
  29.                         url: 'data.php',
  30.                         type: 'POST',
  31.                         data: {
  32.                                 res: 1
  33.                         },
  34.                         success: function(data){
  35.                                 $('#result').html(data);
  36.                         }
  37.                 });
  38.         }
  39. });
There you have it we successfully created a Get Multiple Checkbox Data Using jQuery. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!

Add new comment