How to Filter Between Two Dates using jQuery with PHP/MySQLi

Getting Started

First, we need the jQuery library and also Bootstrap for a better design to our app. I've included these files in the downloadable of this tutorial but if you want, you can download them yourself using the links below: For Bootstrap For jQuery Take note that we are gonna be using HTML5's input date.

Creating our Database

Next, we create the database that we are going to filter in this tutorial. I've included a SQL file in the downloadable of this tutorial. All you have to do is import the said file. If you have no idea on how to import, please visit my tutorial How import .sql file to restore MySQL database.

Displaying our Table

Next, we are going to display table where we display our data. Create a new file, name it as index.html and paste the codes below.
  1. <!DOCTYPE html>
  2.         <meta charset="utf-8">
  3.         <title>How to Filter Between Two Dates using jQuery with PHP/MySQLi</title>
  4.         <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
  5. </head>
  6. <div class="container">
  7.         <h1 class="page-header text-center">Filter Between Two Dates using jQuery</h1>
  8.         <div class="row">
  9.                 <div class="col-sm-8 col-sm-offset-2">
  10.                         <form class="form-inline">
  11.                                 <input type="date" class="form-control" id="date_start" required>
  12.                                 <input type="date" class="form-control" id="date_end" required>
  13.                                 <button type="button" id="filter" class="btn btn-primary">Filter</button>
  14.                                 <button type="button" id="return" class="btn btn-primary">Return</button>
  15.                         </form>
  16.                         <br>
  17.                         <table class="table table-bordered table-striped">
  18.                                 <thead>
  19.                                         <th>ID</th>
  20.                                         <th>Firstname</th>
  21.                                         <th>Lastname</th>
  22.                                         <th>Address</th>
  23.                                         <th>Gender</th>
  24.                                         <th>Birthday</th>
  25.                                 </thead>
  26.                                 <tbody id="tbody">
  27.                                 </tbody>
  28.                         </table>
  29.                 </div>
  30.         </div>
  31. </div>
  32.  
  33. <script src="jquery.min.js"></script>
  34. <script src="bootstrap/js/bootstrap.min.js"></script>
  35. <script src="app.js"></script>
  36. </body>
  37. </html>

Creating our jQuery Scripts

Next, we create our jquery scripts for all our jquery functions and request. Create a new file, name it as app.js and paste the codes below.
  1. $(document).ready(function(){
  2.         fetch();
  3.  
  4.         //clicking the filter button
  5.         $('#filter').click(function(e){
  6.                 var date_start = $('#date_start').val();
  7.                 var date_end = $('#date_end').val();
  8.                 $.ajax({
  9.                         method: 'POST',
  10.                         url: 'filter.php',
  11.                         data: {
  12.                                 date_start: date_start,
  13.                                 date_end: date_end,
  14.                         },
  15.                         dataType: 'json',
  16.                         success: function(response){
  17.                                 if(response.error){
  18.                                         $('#tbody').html('<tr><td colspan="6" align="center">No data matches your filter</td></tr>');
  19.                                 }
  20.                                 else{
  21.                                         $('#tbody').html(response.data);
  22.                                 }
  23.                         }
  24.                 });
  25.         });
  26.  
  27.         $('#return').click(function(){
  28.                 $('#date_start').val('');
  29.                 $('#date_end').val('');
  30.                 fetch();
  31.         });
  32.  
  33. });
  34. function fetch(){
  35.         $.ajax({
  36.                 method: 'POST',
  37.                 url: 'fetch.php',
  38.                 dataType: 'json',
  39.                 success: function(response){
  40.                         $('#tbody').html(response);
  41.                 }
  42.         });
  43. }

Creating our Fetch Table Data Script

Next, we create our script that will fetch data from our MySQL Database as our initial table data. Create a new file, name it as fetch.php.
  1. <?php
  2.         //connection
  3.         $conn = new mysqli('localhost', 'root', '', 'mydatabase');
  4.  
  5.         $output = '';
  6.  
  7.         $sql = "SELECT * FROM members";
  8.         $query = $conn->query($sql);
  9.  
  10.         while($row = $query->fetch_assoc()){
  11.                 $output .= "
  12.                         <tr>
  13.                                 <td>".$row['id']."</td>
  14.                                 <td>".$row['firstname']."</td>
  15.                                 <td>".$row['lastname']."</td>
  16.                                 <td>".$row['address']."</td>
  17.                                 <td>".$row['gender']."</td>
  18.                                 <td>".date('M d, Y', strtotime($row['birthday']))."</td>
  19.                         </tr>
  20.                 ";
  21.         }
  22.  
  23.         echo json_encode($output);
  24.        
  25. ?>

Creating our Filter Script

Lastly, we create our script that will filter data on our table depending on the dates we enter. We name this file as filter.php.
  1. <?php
  2.         //connection
  3.         $conn = new mysqli('localhost', 'root', '', 'mydatabase');
  4.  
  5.         $start = date('Y-m-d', strtotime($_POST['date_start']));
  6.         $end = date('Y-m-d', strtotime($_POST['date_end']));
  7.        
  8.         $output = array('error' => false, 'data' => '');
  9.  
  10.         $sql = "SELECT * FROM members WHERE birthday BETWEEN '$start' AND '$end'";
  11.         $query = $conn->query($sql);
  12.  
  13.         if($query->num_rows > 0){
  14.                 while($row = $query->fetch_assoc()){
  15.                         $output['data'] .= "
  16.                                 <tr>
  17.                                         <td>".$row['id']."</td>
  18.                                         <td>".$row['firstname']."</td>
  19.                                         <td>".$row['lastname']."</td>
  20.                                         <td>".$row['address']."</td>
  21.                                         <td>".$row['gender']."</td>
  22.                                         <td>".date('M d, Y', strtotime($row['birthday']))."</td>
  23.                                 </tr>
  24.                         ";
  25.                 }
  26.         }
  27.         else{
  28.                 $output['error'] = true;
  29.         }
  30.  
  31.         echo json_encode($output);
  32. ?>
That ends this tutorial. Happy Coding :)

Add new comment