• We just launched and are currently in beta. Join us as we build and grow the community.

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

nofe

Passive Income Architect
N Rep
0
0
0
Rep
0
N Vouches
0
0
0
Vouches
0
Posts
164
Likes
57
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
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. <html

    >
  3. <head

    >
  4. <meta

    charset

    =

    "utf-8"

    >
  5. <title

    >

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

    title

    >
  6. <link

    rel

    =

    "stylesheet"

    type

    =

    "text/css"

    href

    =

    "bootstrap/css/bootstrap.min.css"

    >
  7. </

    head

    >
  8. <body

    >
  9. <div

    class

    =

    "container"

    >
  10. <h1

    class

    =

    "page-header text-center"

    >

    Filter Between Two Dates using jQuery</

    h1

    >
  11. <div

    class

    =

    "row"

    >
  12. <div

    class

    =

    "col-sm-8 col-sm-offset-2"

    >
  13. <form

    class

    =

    "form-inline"

    >
  14. <input

    type

    =

    "date"

    class

    =

    "form-control"

    id

    =

    "date_start"

    required>
  15. <input

    type

    =

    "date"

    class

    =

    "form-control"

    id

    =

    "date_end"

    required>
  16. <button

    type

    =

    "button"

    id

    =

    "filter"

    class

    =

    "btn btn-primary"

    >

    Filter</

    button

    >
  17. <button

    type

    =

    "button"

    id

    =

    "return"

    class

    =

    "btn btn-primary"

    >

    Return</

    button

    >
  18. </

    form

    >
  19. <br

    >
  20. <table

    class

    =

    "table table-bordered table-striped"

    >
  21. <thead

    >
  22. <th

    >

    ID</

    th

    >
  23. <th

    >

    Firstname</

    th

    >
  24. <th

    >

    Lastname</

    th

    >
  25. <th

    >

    Address</

    th

    >
  26. <th

    >

    Gender</

    th

    >
  27. <th

    >

    Birthday</

    th

    >
  28. </

    thead

    >
  29. <tbody

    id

    =

    "tbody"

    >
  30. </

    tbody

    >
  31. </

    table

    >
  32. </

    div

    >
  33. </

    div

    >
  34. </

    div

    >

  35. <script

    src

    =

    "jquery.min.js"

    ></

    script

    >
  36. <script

    src

    =

    "bootstrap/js/bootstrap.min.js"

    ></

    script

    >
  37. <script

    src

    =

    "app.js"

    ></

    script

    >
  38. </

    body

    >
  39. </

    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. //clicking the filter button
  4. $(

    '#filter'

    )

    .click

    (

    function

    (

    e)

    {
  5. var

    date_start =

    $(

    '#date_start'

    )

    .val

    (

    )

    ;
  6. var

    date_end =

    $(

    '#date_end'

    )

    .val

    (

    )

    ;
  7. $.ajax

    (

    {
  8. method:

    'POST'

    ,
  9. url:

    'filter.php'

    ,
  10. data:

    {
  11. date_start:

    date_start,
  12. date_end:

    date_end,
  13. }

    ,
  14. dataType:

    'json'

    ,
  15. success:

    function

    (

    response)

    {
  16. if

    (

    response.error

    )

    {
  17. $(

    '#tbody'

    )

    .html

    (

    '<tr><td colspan="6" align="center">No data matches your filter</td></tr>'

    )

    ;
  18. }
  19. else

    {
  20. $(

    '#tbody'

    )

    .html

    (

    response.data

    )

    ;
  21. }
  22. }
  23. }

    )

    ;
  24. }

    )

    ;

  25. $(

    '#return'

    )

    .click

    (

    function

    (

    )

    {
  26. $(

    '#date_start'

    )

    .val

    (

    ''

    )

    ;
  27. $(

    '#date_end'

    )

    .val

    (

    ''

    )

    ;
  28. fetch(

    )

    ;
  29. }

    )

    ;

  30. }

    )

    ;
  31. function

    fetch(

    )

    {
  32. $.ajax

    (

    {
  33. method:

    'POST'

    ,
  34. url:

    'fetch.php'

    ,
  35. dataType:

    'json'

    ,
  36. success:

    function

    (

    response)

    {
  37. $(

    '#tbody'

    )

    .html

    (

    response)

    ;
  38. }
  39. }

    )

    ;
  40. }

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. $output

    =

    ''

    ;

  5. $sql

    =

    "SELECT * FROM members"

    ;
  6. $query

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;

  7. while

    (

    $row

    =

    $query

    ->

    fetch_assoc

    (

    )

    )

    {
  8. $output

    .=

    "
  9. <tr>
  10. <td>"

    .

    $row

    [

    'id'

    ]

    .

    "</td>
  11. <td>"

    .

    $row

    [

    'firstname'

    ]

    .

    "</td>
  12. <td>"

    .

    $row

    [

    'lastname'

    ]

    .

    "</td>
  13. <td>"

    .

    $row

    [

    'address'

    ]

    .

    "</td>
  14. <td>"

    .

    $row

    [

    'gender'

    ]

    .

    "</td>
  15. <td>"

    .

    date

    (

    'M d, Y'

    ,

    strtotime

    (

    $row

    [

    'birthday'

    ]

    )

    )

    .

    "</td>
  16. </tr>
  17. "

    ;
  18. }

  19. echo

    json_encode

    (

    $output

    )

    ;

  20. ?>

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. $start

    =

    date

    (

    'Y-m-d'

    ,

    strtotime

    (

    $_POST

    [

    'date_start'

    ]

    )

    )

    ;
  5. $end

    =

    date

    (

    'Y-m-d'

    ,

    strtotime

    (

    $_POST

    [

    'date_end'

    ]

    )

    )

    ;

  6. $output

    =

    array

    (

    'error'

    =>

    false

    ,

    'data'

    =>

    ''

    )

    ;

  7. $sql

    =

    "SELECT * FROM members WHERE birthday BETWEEN '$start

    ' AND '$end

    '"

    ;
  8. $query

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;

  9. if

    (

    $query

    ->

    num_rows

    >

    0

    )

    {
  10. while

    (

    $row

    =

    $query

    ->

    fetch_assoc

    (

    )

    )

    {
  11. $output

    [

    'data'

    ]

    .=

    "
  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. else

    {
  24. $output

    [

    'error'

    ]

    =

    true

    ;
  25. }

  26. echo

    json_encode

    (

    $output

    )

    ;
  27. ?>

That ends this tutorial. Happy Coding :)

 

452,496

327,690

327,698

Top