PHP - Submit Multiple Checkbox Data

In this tutorial we will create a Submit Multiple Checkbox Data using PHP. PHP is a server-side scripting language designed primarily for web development. It is a lean and consistent way to access databases. This means developers can write portable code much easier. It is mostly used by a newly coders for its user friendly environment. So Let's do the coding...

Getting 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_select, 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 = mysqli_connect('localhost', 'root', '', 'db_select');
  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 you text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html>
  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 - Submit Multiple Checkbox Data</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <button type="button" class="btn btn-success" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add Member</button>
  18.                 <br /><br />
  19.                 <table class="table table-bordered">
  20.                         <thead class="alert-info">
  21.                                 <tr>
  22.                                         <th>Firstname</th>
  23.                                         <th>Lastname</th>
  24.                                         <th>Address</th>
  25.                                         <th>Skill</th>
  26.                                 </tr>
  27.                         </thead>
  28.                         <tbody style="background-color:#fff;">
  29.                                 <?php
  30.                                         require 'conn.php';
  31.                                         $stmt = mysqli_stmt_init($conn);
  32.                                         if(mysqli_stmt_prepare($stmt, "SELECT * FROM `member`")){
  33.                                                 mysqli_stmt_execute($stmt);
  34.                                                 mysqli_stmt_bind_result($stmt, $mem_id, $firstname, $lastname, $address, $skill);
  35.                                                 while(mysqli_stmt_fetch($stmt)){
  36.                                 ?>
  37.                                 <tr>
  38.                                         <td><?php echo $firstname?></td>
  39.                                         <td><?php echo $lastname?></td>
  40.                                         <td><?php echo $address?></td>
  41.                                         <td><?php echo $skill?></td>
  42.                                 </tr>
  43.                                 <?php
  44.                                                 }
  45.                                                 mysqli_stmt_close($stmt);
  46.                                         }
  47.                                 ?>
  48.                         </tbody>
  49.                 </table>
  50.         </div>
  51.         <div class="modal fade" id="form_modal" tabindex="-1" role="dialog" aria-hidden="true">
  52.                 <div class="modal-dialog" role="document">
  53.                         <form action="save_data.php" method="POST">
  54.                                 <div class="modal-content">
  55.                                         <div class="modal-body">
  56.                                                 <div class="col-md-3"></div>
  57.                                                 <div class="col-md-6">
  58.                                                         <div class="form-group">
  59.                                                                 <label>Firstname:</label>
  60.                                                                 <input type="text" class="form-control" name="firstname" required="required"/>
  61.                                                         </div>
  62.                                                         <div class="form-group">
  63.                                                                 <label>Lastname:</label>
  64.                                                                 <input type="text" class="form-control" name="lastname" required="required"/>
  65.                                                         </div>
  66.                                                         <div class="form-group">
  67.                                                                 <label>Address:</label>
  68.                                                                 <input type="text" class="form-control" name="address" required="required"/>
  69.                                                         </div>
  70.                                                         <div>
  71.                                                                 <label>Skill:</label>
  72.                                                                 <br style="clear:both;"/>
  73.                                                                 <div class="col-md-6">
  74.                                                                         <div class="checkbox">
  75.                                                                                 <label><input type="checkbox" name="skill[]" value="C" >C</label>
  76.                                                                         </div>
  77.                                                                         <div class="checkbox">
  78.                                                                                 <label><input type="checkbox" name="skill[]" value="C++">C++</label>
  79.                                                                         </div>
  80.                                                                         <div class="checkbox">
  81.                                                                                 <label><input type="checkbox" name="skill[]" value="C#">C#</label>
  82.                                                                         </div>
  83.                                                                         <div class="checkbox">
  84.                                                                                 <label><input type="checkbox" name="skill[]" value="PHP">PHP</label>
  85.                                                                         </div>
  86.                                                                         <div class="checkbox">
  87.                                                                                 <label><input type="checkbox" name="skill[]" value="PASCAL">PASCAL</label>
  88.                                                                         </div>
  89.                                                                 </div>
  90.                                                                 <div class="col-md-6">
  91.                                                                         <div class="checkbox">
  92.                                                                                 <label><input type="checkbox" name="skill[]" value="Visual Basic">Visual Basic</label>
  93.                                                                         </div>
  94.                                                                         <div class="checkbox">
  95.                                                                                 <label><input type="checkbox" name="skill[]" value="Python">Python</label>
  96.                                                                         </div>
  97.                                                                         <div class="checkbox">
  98.                                                                                 <label><input type="checkbox" name="skill[]" value="Java">Java</label>
  99.                                                                         </div>
  100.                                                                         <div class="checkbox">
  101.                                                                                 <label><input type="checkbox" name="skill[]" value="Ruby">Ruby</label>
  102.                                                                         </div>
  103.                                                                         <div class="checkbox">
  104.                                                                                 <label><input type="checkbox" name="skill[]" value="Swift">Swift</label>
  105.                                                                         </div>
  106.                                                                 </div>
  107.                                                         </div>
  108.                                                 </div>
  109.                                         </div>
  110.                                         <div style="clear:both;"></div>
  111.                                         <div class="modal-footer">
  112.                                                 <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  113.                                                 <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  114.                                         </div>
  115.                                 </div>
  116.                         </form>
  117.                 </div>
  118.         </div>
  119. </body>
  120. <script src="js/jquery-3.2.1.min.js"></script>
  121. <script src="js/bootstrap.js"></script>
  122. </html>

Creating Main Function

This code contains the php query of the application. This code will store the checkboxes data to the database server. To do that just copy and write this block of codes inside the text editor, then save it as save_data.php.
  1. <?php
  2.         ob_start();
  3.         require_once 'conn.php';
  4.         if(ISSET($_POST['save'])){
  5.                 $firstname = $_POST['firstname'];
  6.                 $lastname = $_POST['lastname'];
  7.                 $address = $_POST['address'];
  8.                 $skill = addslashes(implode(", ", $_POST['skill']));
  9.  
  10.                 $stmt = mysqli_stmt_init($conn);
  11.                 if(mysqli_stmt_prepare($stmt, "INSERT INTO `member` VALUES('', ?, ?, ?, ?)")){
  12.                         mysqli_stmt_bind_param($stmt, "ssss", $firstname, $lastname, $address, $skill);
  13.                         mysqli_stmt_execute($stmt);
  14.                         mysqli_stmt_close($stmt);
  15.                        
  16.                         header("location:index.php");
  17.                 }
  18.                
  19.         }
  20. ?>
There you have it we successfully created Submit Multiple Checkbox Data using PHP. 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