PHP - Select and Deselect Using jQuery

In this tutorial we will create a Select and Deselect Using jQuery. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. 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_subject, 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_subject');
  3.        
  4.         if(!$conn){
  5.                 die("Error: Failed to 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 - Select and Deselect Using jQuery</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                
  18.                 <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add Subject</button>
  19.                 <hr style="border-top:1px solid #ccc;"/>
  20.                 <div class="checkbox">
  21.                         <label class="text-primary">
  22.                                 <input type="checkbox" id="checkall"/> Select/Deselect All
  23.                         </label>
  24.                 </div>
  25.                
  26.                 <br />
  27.                 <h2 class="text-success">My Class Subject</h2>
  28.                 <div class="col-md-1"></div>
  29.                 <div class="col-md-10">
  30.                         <div class="checkbox" id="data"></div>
  31.                 </div>
  32.                
  33.                 <button type="button" id="remove" class="btn btn-danger pull-right">Remove <span class="badge badge-light"><label id="counter">0</label></span></button>
  34.                 <br style="clear:both;"/>
  35.         </div>
  36.         <div class="modal fade" id="form_modal" tabindex="-1" role="dialog" aria-hidden="true">
  37.                 <div class="modal-dialog" role="document">
  38.                         <form action="save_query.php" method="POST">
  39.                                 <div class="modal-content">
  40.                                         <div class="modal-body">
  41.                                                 <div class="col-md-2"></div>
  42.                                                 <div class="col-md-8">
  43.                                                         <div class="form-group">
  44.                                                                 <label>Subject Code</label>
  45.                                                                 <input class="form-control" type="text" name="subject_code">
  46.                                                         </div>
  47.                                                         <div class="form-group">
  48.                                                                 <label>Subject</label>
  49.                                                                 <input class="form-control" type="text" name="subject" />
  50.                                                         </div>
  51.                                                 </div>
  52.                                         </div>
  53.                                         <div style="clear:both;"></div>
  54.                                         <div class="modal-footer">
  55.                                                 <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  56.                                                 <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  57.                                         </div>
  58.                                 </div>
  59.                         </form>
  60.                 </div>
  61.         </div>
  62. </body>
  63. <script src="js/jquery-3.2.1.min.js"></script>
  64. <script src="js/bootstrap.js"></script>
  65. <script src="js/script.js"></script>
  66. </html>

Creating PHP Query

This code contains the php query of the application. This code will store the data inputs to the php server and remove base on the selected checkbox, and also display data 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(ISSET($_POST['save'])){
  5.                 $subject_code = $_POST['subject_code'];
  6.                 $subject = $_POST['subject'];
  7.                
  8.                 $conn->query("INSERT INTO `subject` VALUES('', '$subject_code', '$subject')");
  9.                
  10.                 header('location:index.php');
  11.         }
  12. ?>
data.php
  1. <?php
  2.         require 'conn.php';
  3.        
  4.         $query = $conn->query("SELECT * FROM `subject`");
  5.         while($fetch = $query->fetch_array()){
  6. ?>
  7.         <label style="font-size:30px; margin-left:5px; margin-right:5px;">
  8.                 <input type="checkbox" name="subject_id[]" value="<?php echo $fetch['subject_id']?>" class="checkitem"/> <?php echo $fetch['subject_code']?>
  9.         </label>
  10. <?php
  11.         }
  12. ?>
remove.php
  1. <?php
  2.         require_once 'conn.php';
  3.  
  4.         if(ISSET($_POST['subject_id'])){
  5.                 foreach($_POST['subject_id'] as $key){
  6.                         $conn->query("DELETE FROM `subject` WHERE `subject_id` = '$key'");
  7.                 }
  8.         }
  9. ?>

Creating jQuery Script

This is where the code that uses ajax request been used. This code will make the checkbox to toggle all the checboxed when it is checked. 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.  
  5.         $('#checkall').change(function(){
  6.                 $(".checkitem").prop("checked", $(this).prop("checked"));
  7.                 $('#counter').html($(".checkitem:checked").length);
  8.         });
  9.        
  10.         $(document).change('.checkitem', function(){
  11.                 if($(".checkitem:checked").length == $(".checkitem").length){
  12.                         $("#checkall").prop("checked", true);
  13.                 }else{
  14.                         $("#checkall").prop("checked", false);
  15.                 }
  16.                
  17.                 $('#counter').html($(".checkitem:checked").length);
  18.         });
  19.        
  20.        
  21.         $('#remove').on('click', function(){
  22.                 if(confirm("Are you sure you want to delete this?")){
  23.                         var subject_id = [];
  24.                         $('.checkitem:checkbox:checked').each(function(i){
  25.                                 subject_id[i] = $(this).val();
  26.                         });
  27.                        
  28.                         if(subject_id.length == 0){
  29.                                 alert("Please select one or more to delete!");
  30.                         }else{
  31.                                 $.ajax({
  32.                                         url: 'remove.php',
  33.                                         type: 'POST',
  34.                                         data: {subject_id: subject_id},
  35.                                         success: function(){
  36.                                                 alert("Subject Remove!");
  37.                                                 DisplayData();
  38.                                         }
  39.                                 });
  40.                         }
  41.                        
  42.                 }
  43.         });
  44.        
  45.         function DisplayData(){
  46.                 $.ajax({
  47.                         url: 'data.php',
  48.                         type: 'POST',
  49.                         data: {
  50.                                 res: 1
  51.                         },
  52.                         success: function(data){
  53.                                 $('#data').html(data);
  54.                         }
  55.                 });
  56.         }
  57. });
There you have it we successfully created Select and Deselect 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