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

PHP CRUD without Refresh/Reload using Ajax and DataTables Tutorial

pumpingnick

Cheeky Mastermind
P Rep
0
0
0
Rep
0
P Vouches
0
0
0
Vouches
0
Posts
89
Likes
72
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial, you will learn how to create or develop a PHP CRUD Operations w/o Pagre Refresh/Reloading using Ajax and DataTables. Here, we will enlist the data from the database using the server-side processing of DataTables. This tutorial is helpful especially for optimizing the page load of your future PHP Project.

The main goal of the simple web app that we will be creating is to display the data/records from the database into a table using DataTables which means it has a sort, filter, limit, and pagination functions. We will be using Ajax to process the new, updated, and delete data. Lastly, in each process, the table must update its data without leaving the current page/reloading/refreshing.

Getting Started

In this tutorial, I will be using the following

  • XAMPP as my local web-server to run my PHP scripts.
  • Bootstrap 5 for the user interface or the design of the web app
  • jQuery to use Ajax
  • DataTables andd please include the Button Extention

Compile the plugins/libraries files in a single foleder in your XAMPP's htdocs directory. Please open your XAMPP's Control Panel and start the Apache and MySQL.

Creating the Database

In your web browser, browse the PHPMyAdmin i.e. http://localhost/phpmyadmin

and create a new database naming dummy_db. Then navigate to the database SQL Tab and paster the SQL Script below to create our database table and columns.

  1. CREATE

    TABLE

    `authors`

    (
  2. `id`

    int

    (

    11

    )

    NOT

    NULL

    ,
  3. `first_

    name`

    varchar

    (

    50

    )

    COLLATE

    utf8_unicode_ci NOT

    NULL

    ,
  4. `last_

    name`

    varchar

    (

    50

    )

    COLLATE

    utf8_unicode_ci NOT

    NULL

    ,
  5. `email`

    varchar

    (

    100

    )

    COLLATE

    utf8_unicode_ci NOT

    NULL

    ,
  6. `birthdate`

    date

    NOT

    NULL

    ,
  7. `added`

    timestamp

    NOT

    NULL

    DEFAULT

    current_timestamp

    (

    )
  8. )

    ENGINE

    =

    InnoDB

    DEFAULT

    CHARSET

    =

    utf8 COLLATE

    =

    utf8_unicode_ci;

  9. ALTER

    TABLE

    `authors`
  10. ADD

    PRIMARY KEY

    (

    `id`

    )

    ;

  11. ALTER

    TABLE

    `authors`
  12. MODIFY

    `id`

    int

    (

    11

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,

    AUTO_INCREMENT

    =

    102

    ;

Creating our Database Connection

In you source code folder, create a new PHP file naming connect.php. Copy/Paste the script below. The script will create a connection to our database.

  1. <?php
  2. $host

    =

    "localhost"

    ;
  3. $username

    =

    "root"

    ;
  4. $password

    =

    ""

    ;
  5. $database

    =

    "dummy_db"

    ;

  6. $conn

    =

    new

    mysqli(

    $host

    ,

    $username

    ,

    $password

    ,

    $database

    )

    ;
  7. if

    (

    !

    $conn

    )

    {
  8. echo

    "Database connection failed. Error:"

    .

    $conn

    ->

    error

    ;
  9. exit

    ;
  10. }
  11. ?>

Creating the Interface

Create a new PHP file naming index.php. Paste the following script, to create our web app's interface. Please make sure that plugins/libraries path are correct in your end for both CSS Links and External JavaScript.

  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    >

  3. <head

    >
  4. <meta

    charset

    =

    "UTF-8"

    >
  5. <meta

    http-equiv

    =

    "X-UA-Compatible"

    content

    =

    "IE=edge"

    >
  6. <meta

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1.0"

    >
  7. <title

    >

    PHP CRUD using Ajax and DataTables</

    title

    >
  8. <link

    rel

    =

    "stylesheet"

    href

    =

    "css/bootstrap.css"

    >
  9. <link

    rel

    =

    "stylesheet"

    href

    =

    "DataTables/datatables.min.css"

    >
  10. <link

    rel

    =

    "stylesheet"

    href

    =

    "https://cdn.datatables.net/buttons/2.0.0/css/buttons.dataTables.min.css"

    >
  11. <script

    src

    =

    "js/jquery-3.6.0.min.js"

    ></

    script

    >
  12. <script

    src

    =

    "js/bootstrap.js"

    ></

    script

    >
  13. <script

    src

    =

    "DataTables/datatables.min.js"

    ></

    script

    >
  14. <script

    src

    =

    "https://cdn.datatables.net/buttons/2.0.0/js/dataTables.buttons.min.js"

    ></

    script

    >
  15. <script

    src

    =

    "js/script.js"

    ></

    script

    >
  16. <style

    >
  17. button.dt-button.btn-primary{
  18. background:var(--bs-primary)!important;
  19. color:white;
  20. }
  21. </

    style

    >
  22. </

    head

    >

  23. <body

    class

    =

    ""

    >
  24. <nav class

    =

    "navbar navbar-expand-lg navbar-light bg-dark bg-gradient"

    >
  25. <div

    class

    =

    "container"

    >
  26. <a

    class

    =

    "navbar-brand text-light text-shadow"

    href

    =

    "//www.sourcecodester"

    target

    =

    "_blank"

    >

    SourceCodester</

    a

    >
  27. </

    div

    >
  28. </

    nav>

  29. <div

    class

    =

    "container py-5 h-100"

    >
  30. <div

    class

    =

    "row"

    >
  31. <div

    class

    =

    "col-lg-12"

    >
  32. <h3

    class

    =

    "text-center"

    ><b

    >

    PHP CRUD w/o Refresh using Ajax and DataTable</

    b

    ></

    h3

    >
  33. </

    div

    >
  34. </

    div

    >
  35. <hr

    >
  36. <div

    class

    =

    "row"

    >
  37. <div

    class

    =

    "col-md-12"

    id

    =

    "msg"

    ></

    div

    >
  38. </

    div

    >
  39. <div

    class

    =

    "row"

    >
  40. <div

    class

    =

    "col-lg-12"

    >
  41. <table

    class

    =

    "table table-hover table-bordered table-striped"

    id

    =

    "authors-tbl"

    >
  42. <thead

    >
  43. <tr

    class

    =

    "bg-dark text-light bg-gradient bg-opacity-150"

    >
  44. <th

    class

    =

    "px-1 py-1 text-center"

    >

    #</

    th

    >
  45. <th

    class

    =

    "px-1 py-1 text-center"

    >

    First Name</

    th

    >
  46. <th

    class

    =

    "px-1 py-1 text-center"

    >

    Last Name</

    th

    >
  47. <th

    class

    =

    "px-1 py-1 text-center"

    >

    Email</

    th

    >
  48. <th

    class

    =

    "px-1 py-1 text-center"

    >

    Birth Date</

    th

    >
  49. <th

    class

    =

    "px-1 py-1 text-center"

    >

    Action</

    th

    >
  50. </

    tr

    >
  51. </

    thead

    >
  52. <tfoot

    >
  53. <tr

    class

    =

    "bg-dark text-light bg-gradient bg-opacity-150"

    >
  54. <th

    class

    =

    "px-1 py-1 text-center"

    >

    #</

    th

    >
  55. <th

    class

    =

    "px-1 py-1 text-center"

    >

    First Name</

    th

    >
  56. <th

    class

    =

    "px-1 py-1 text-center"

    >

    Last Name</

    th

    >
  57. <th

    class

    =

    "px-1 py-1 text-center"

    >

    Email</

    th

    >
  58. <th

    class

    =

    "px-1 py-1 text-center"

    >

    Birth Date</

    th

    >
  59. <th

    class

    =

    "px-1 py-1 text-center"

    >

    Action</

    th

    >
  60. </

    tr

    >
  61. </

    tfoot

    >
  62. </

    table

    >
  63. </

    div

    >
  64. </

    div

    >
  65. </

    div

    >
  66. <!-- Add Modal -->
  67. <div

    class

    =

    "modal fade"

    id

    =

    "add_modal"

    data-bs-backdrop=

    "static"

    >
  68. <div

    class

    =

    "modal-dialog"

    >
  69. <div

    class

    =

    "modal-content"

    >
  70. <div

    class

    =

    "modal-header"

    >
  71. <h5

    class

    =

    "modal-title"

    >

    Add Author</

    h5

    >
  72. <button

    type

    =

    "button"

    class

    =

    "btn-close"

    data-bs-dismiss=

    "modal"

    aria-label

    =

    "Close"

    ></

    button

    >
  73. </

    div

    >
  74. <div

    class

    =

    "modal-body"

    >
  75. <div

    class

    =

    "container-fluid"

    >
  76. <form

    action

    =

    ""

    id

    =

    "new-author-frm"

    >
  77. <div

    class

    =

    "form-group"

    >
  78. <label

    for

    =

    "first_name"

    class

    =

    "control-label"

    >

    First Name</

    label

    >
  79. <input

    type

    =

    "text"

    class

    =

    "form-control rounded-0"

    id

    =

    "first_name"

    name

    =

    "first_name"

    required>
  80. </

    div

    >
  81. <div

    class

    =

    "form-group"

    >
  82. <label

    for

    =

    "last_name"

    class

    =

    "control-label"

    >

    Last Name</

    label

    >
  83. <input

    type

    =

    "text"

    class

    =

    "form-control rounded-0"

    id

    =

    "last_name"

    name

    =

    "last_name"

    required>
  84. </

    div

    >
  85. <div

    class

    =

    "form-group"

    >
  86. <label

    for

    =

    "email"

    class

    =

    "control-label"

    >

    Email</

    label

    >
  87. <input

    type

    =

    "text"

    class

    =

    "form-control rounded-0"

    id

    =

    "email"

    name

    =

    "email"

    required>
  88. </

    div

    >
  89. <div

    class

    =

    "form-group"

    >
  90. <label

    for

    =

    "birthdate"

    class

    =

    "control-label"

    >

    Date of Birth</

    label

    >
  91. <input

    type

    =

    "date"

    class

    =

    "form-control rounded-0"

    id

    =

    "birthdate"

    name

    =

    "birthdate"

    required>
  92. </

    div

    >
  93. </

    form

    >
  94. </

    div

    >
  95. </

    div

    >
  96. <div

    class

    =

    "modal-footer"

    >
  97. <button

    type

    =

    "submit"

    class

    =

    "btn btn-primary"

    form=

    "new-author-frm"

    >

    Save</

    button

    >
  98. <button

    type

    =

    "button"

    class

    =

    "btn btn-secondary"

    data-bs-dismiss=

    "modal"

    >

    Close</

    button

    >
  99. </

    div

    >
  100. </

    div

    >
  101. </

    div

    >
  102. </

    div

    >
  103. <!-- /Add Modal -->
  104. <!-- Edit Modal -->
  105. <div

    class

    =

    "modal fade"

    id

    =

    "edit_modal"

    data-bs-backdrop=

    "static"

    >
  106. <div

    class

    =

    "modal-dialog"

    >
  107. <div

    class

    =

    "modal-content"

    >
  108. <div

    class

    =

    "modal-header"

    >
  109. <h5

    class

    =

    "modal-title"

    >

    Edit Author's Details</

    h5

    >
  110. <button

    type

    =

    "button"

    class

    =

    "btn-close"

    data-bs-dismiss=

    "modal"

    aria-label

    =

    "Close"

    ></

    button

    >
  111. </

    div

    >
  112. <div

    class

    =

    "modal-body"

    >
  113. <div

    class

    =

    "container-fluid"

    >
  114. <form

    action

    =

    ""

    id

    =

    "edit-author-frm"

    >
  115. <input

    type

    =

    "hidden"

    name

    =

    "id"

    >
  116. <div

    class

    =

    "form-group"

    >
  117. <label

    for

    =

    "first_name"

    class

    =

    "control-label"

    >

    First Name</

    label

    >
  118. <input

    type

    =

    "text"

    class

    =

    "form-control rounded-0"

    id

    =

    "first_name"

    name

    =

    "first_name"

    required>
  119. </

    div

    >
  120. <div

    class

    =

    "form-group"

    >
  121. <label

    for

    =

    "last_name"

    class

    =

    "control-label"

    >

    Last Name</

    label

    >
  122. <input

    type

    =

    "text"

    class

    =

    "form-control rounded-0"

    id

    =

    "last_name"

    name

    =

    "last_name"

    required>
  123. </

    div

    >
  124. <div

    class

    =

    "form-group"

    >
  125. <label

    for

    =

    "email"

    class

    =

    "control-label"

    >

    Email</

    label

    >
  126. <input

    type

    =

    "text"

    class

    =

    "form-control rounded-0"

    id

    =

    "email"

    name

    =

    "email"

    required>
  127. </

    div

    >
  128. <div

    class

    =

    "form-group"

    >
  129. <label

    for

    =

    "birthdate"

    class

    =

    "control-label"

    >

    Date of Birth</

    label

    >
  130. <input

    type

    =

    "date"

    class

    =

    "form-control rounded-0"

    id

    =

    "birthdate"

    name

    =

    "birthdate"

    required>
  131. </

    div

    >
  132. </

    form

    >
  133. </

    div

    >
  134. </

    div

    >
  135. <div

    class

    =

    "modal-footer"

    >
  136. <button

    type

    =

    "submit"

    class

    =

    "btn btn-primary"

    form=

    "edit-author-frm"

    >

    Save</

    button

    >
  137. <button

    type

    =

    "button"

    class

    =

    "btn btn-secondary"

    data-bs-dismiss=

    "modal"

    >

    Close</

    button

    >
  138. </

    div

    >
  139. </

    div

    >
  140. </

    div

    >
  141. </

    div

    >
  142. <!-- /Edit Modal -->
  143. <!-- Delete Modal -->
  144. <div

    class

    =

    "modal fade"

    id

    =

    "delete_modal"

    data-bs-backdrop=

    "static"

    >
  145. <div

    class

    =

    "modal-dialog modal-dialog-centered"

    >
  146. <div

    class

    =

    "modal-content"

    >
  147. <div

    class

    =

    "modal-header"

    >
  148. <h5

    class

    =

    "modal-title"

    >

    Confirm</

    h5

    >
  149. </

    div

    >
  150. <div

    class

    =

    "modal-body"

    >
  151. <div

    class

    =

    "container-fluid"

    >
  152. <form

    action

    =

    ""

    id

    =

    "delete-author-frm"

    >
  153. <input

    type

    =

    "hidden"

    name

    =

    "id"

    >
  154. <p

    >

    Are you sure to delete <b

    ><span

    id

    =

    "name"

    ></

    span

    ></

    b

    >

    from the list?</

    p

    >
  155. </

    form

    >
  156. </

    div

    >
  157. </

    div

    >
  158. <div

    class

    =

    "modal-footer"

    >
  159. <button

    type

    =

    "submit"

    class

    =

    "btn btn-danger"

    form=

    "delete-author-frm"

    >

    Yes</

    button

    >
  160. <button

    type

    =

    "button"

    class

    =

    "btn btn-primary"

    data-bs-dismiss=

    "modal"

    >

    No</

    button

    >
  161. </

    div

    >
  162. </

    div

    >
  163. </

    div

    >
  164. </

    div

    >
  165. <!-- /Delete Modal -->
  166. </

    body

    >
  167. </

    html

    >

Creating our JavaScript Functions

The script below contains all of the scripts and functions that initiating the processes of the CRUD Operations. It also includes the DataTables initiation. Save the file as script.js. In my case, the file is located inside the js directory inside my source code folder.

  1. var

    authorsTbl =

    ''

    ;
  2. $(

    function

    (

    )

    {
  3. // draw function [called if the database updates]
  4. function

    draw_data(

    )

    {
  5. if

    (

    $.fn

    .dataTable

    .isDataTable

    (

    '#authors-tbl'

    )

    &&

    authorsTbl !=

    ''

    )

    {
  6. authorsTbl.draw

    (

    true

    )
  7. }

    else

    {
  8. load_data(

    )

    ;
  9. }
  10. }

  11. function

    load_data(

    )

    {
  12. authorsTbl =

    $(

    '#authors-tbl'

    )

    .DataTable

    (

    {
  13. dom:

    '<"row"B>flr<"py-2 my-2"t>ip'

    ,
  14. "processing"

    :

    true

    ,
  15. "serverSide"

    :

    true

    ,
  16. "ajax"

    :

    {
  17. url:

    "./get_authors.php"

    ,
  18. method:

    'POST'
  19. }

    ,
  20. columns:

    [

    {
  21. data:

    'id'

    ,
  22. className:

    'py-0 px-1'
  23. }

    ,
  24. {
  25. data:

    'first_name'

    ,
  26. className:

    'py-0 px-1'
  27. }

    ,
  28. {
  29. data:

    'last_name'

    ,
  30. className:

    'py-0 px-1'
  31. }

    ,
  32. {
  33. data:

    'email'

    ,
  34. className:

    'py-0 px-1'
  35. }

    ,
  36. {
  37. data:

    'birthdate'

    ,
  38. className:

    'py-0 px-1'
  39. }

    ,
  40. {
  41. data:

    null

    ,
  42. orderable:

    false

    ,
  43. className:

    'text-center py-0 px-1'

    ,
  44. render:

    function

    (

    data,

    type,

    row,

    meta)

    {
  45. console.log

    (

    )
  46. return

    '<a class="me-2 btn btn-sm rounded-0 py-0 edit_data btn-primary" href="javascript:void(0)" data-id="'

    +

    (

    row.id

    )

    +

    '">Edit</a><a class="btn btn-sm rounded-0 py-0 delete_data btn-danger" href="javascript:void(0)" data-id="'

    +

    (

    row.id

    )

    +

    '">Delete</a>'

    ;
  47. }
  48. }
  49. ]

    ,
  50. drawCallback:

    function

    (

    settings)

    {
  51. $(

    '.edit_data'

    )

    .click

    (

    function

    (

    )

    {
  52. $.ajax

    (

    {
  53. url:

    'get_single.php'

    ,
  54. data:

    {

    id:

    $(

    this

    )

    .attr

    (

    'data-id'

    )

    }

    ,
  55. method:

    'POST'

    ,
  56. dataType:

    'json'

    ,
  57. error:

    err =>

    {
  58. alert(

    "An error occured while fetching single data"

    )
  59. }

    ,
  60. success:

    function

    (

    resp)

    {
  61. if

    (

    !!

    resp.status

    )

    {
  62. Object

    .keys

    (

    resp.data

    )

    .map

    (

    k =>

    {
  63. if

    (

    $(

    '#edit_modal'

    )

    .find

    (

    'input[name="'

    +

    k +

    '"]'

    )

    .length

    >

    0

    )
  64. $(

    '#edit_modal'

    )

    .find

    (

    'input[name="'

    +

    k +

    '"]'

    )

    .val

    (

    resp.data

    [

    k]

    )
  65. }

    )
  66. $(

    '#edit_modal'

    )

    .modal

    (

    'show'

    )
  67. }

    else

    {
  68. alert(

    "An error occured while fetching single data"

    )
  69. }
  70. }
  71. }

    )
  72. }

    )
  73. $(

    '.delete_data'

    )

    .click

    (

    function

    (

    )

    {
  74. $.ajax

    (

    {
  75. url:

    'get_single.php'

    ,
  76. data:

    {

    id:

    $(

    this

    )

    .attr

    (

    'data-id'

    )

    }

    ,
  77. method:

    'POST'

    ,
  78. dataType:

    'json'

    ,
  79. error:

    err =>

    {
  80. alert(

    "An error occured while fetching single data"

    )
  81. }

    ,
  82. success:

    function

    (

    resp)

    {
  83. if

    (

    !!

    resp.status

    )

    {
  84. $(

    '#delete_modal'

    )

    .find

    (

    'input[name="id"]'

    )

    .val

    (

    resp.data

    [

    'id'

    ]

    )
  85. $(

    '#delete_modal'

    )

    .modal

    (

    'show'

    )
  86. }

    else

    {
  87. alert(

    "An error occured while fetching single data"

    )
  88. }
  89. }
  90. }

    )
  91. }

    )
  92. }

    ,
  93. buttons:

    [

    {
  94. text:

    "Add New"

    ,
  95. className:

    "btn btn-primary py-0"

    ,
  96. action:

    function

    (

    e,

    dt,

    node,

    config)

    {
  97. $(

    '#add_modal'

    )

    .modal

    (

    'show'

    )
  98. }
  99. }

    ]

    ,
  100. "order"

    :

    [
  101. [

    1

    ,

    "asc"

    ]
  102. ]

    ,
  103. initComplete:

    function

    (

    settings)

    {
  104. $(

    '.paginate_button'

    )

    .addClass

    (

    'p-1'

    )
  105. }
  106. }

    )

    ;
  107. }
  108. //Load Data
  109. load_data(

    )
  110. //Saving new Data
  111. $(

    '#new-author-frm'

    )

    .submit

    (

    function

    (

    e)

    {
  112. e.preventDefault

    (

    )
  113. $(

    '#add_modal button'

    )

    .attr

    (

    'disabled'

    ,

    true

    )
  114. $(

    '#add_modal button[form="new-author-frm"]'

    )

    .text

    (

    "saving ..."

    )
  115. $.ajax

    (

    {
  116. url:

    'save_data.php'

    ,
  117. data:

    $(

    this

    )

    .serialize

    (

    )

    ,
  118. method:

    'POST'

    ,
  119. dataType:

    "json"

    ,
  120. error:

    err =>

    {
  121. alert(

    "An error occured. Please chech the source code and try again"

    )
  122. }

    ,
  123. success:

    function

    (

    resp)

    {
  124. if

    (

    !!

    resp.status

    )

    {
  125. if

    (

    resp.status

    ==

    'success'

    )

    {
  126. var

    _el =

    $(

    '<div>'

    )
  127. _el.hide

    (

    )
  128. _el.addClass

    (

    'alert alert-primary alert_msg'

    )
  129. _el.text

    (

    "Data successfulle saved"

    )

    ;
  130. $(

    '#new-author-frm'

    )

    .get

    (

    0

    )

    .reset

    (

    )
  131. $(

    '.modal'

    )

    .modal

    (

    'hide'

    )
  132. $(

    '#msg'

    )

    .append

    (

    _el)
  133. _el.show

    (

    'slow'

    )
  134. draw_data(

    )

    ;
  135. setTimeout(

    (

    )

    =>

    {
  136. _el.hide

    (

    'slow'

    )
  137. .remove

    (

    )
  138. }

    ,

    2500

    )
  139. }

    else

    if

    (

    resp.status

    ==

    'success'

    &&

    !!

    resp.msg

    )

    {
  140. var

    _el =

    $(

    '<div>'

    )
  141. _el.hide

    (

    )
  142. _el.addClass

    (

    'alert alert-danger alert_msg form-group'

    )
  143. _el.text

    (

    resp.msg

    )

    ;
  144. $(

    '#new-author-frm'

    )

    .append

    (

    _el)
  145. _el.show

    (

    'slow'

    )
  146. }

    else

    {
  147. alert(

    "An error occured. Please chech the source code and try again"

    )
  148. }
  149. }

    else

    {
  150. alert(

    "An error occurred. Please check the source code and try again"

    )
  151. }

  152. $(

    '#add_modal button'

    )

    .attr

    (

    'disabled'

    ,

    false

    )
  153. $(

    '#add_modal button[form="new-author-frm"]'

    )

    .text

    (

    "Save"

    )
  154. }
  155. }

    )
  156. }

    )
  157. // Update Data
  158. $(

    '#edit-author-frm'

    )

    .submit

    (

    function

    (

    e)

    {
  159. e.preventDefault

    (

    )
  160. $(

    '#edit_modal button'

    )

    .attr

    (

    'disabled'

    ,

    true

    )
  161. $(

    '#edit_modal button[form="edit-author-frm"]'

    )

    .text

    (

    "saving ..."

    )
  162. $.ajax

    (

    {
  163. url:

    'update_data.php'

    ,
  164. data:

    $(

    this

    )

    .serialize

    (

    )

    ,
  165. method:

    'POST'

    ,
  166. dataType:

    "json"

    ,
  167. error:

    err =>

    {
  168. alert(

    "An error occured. Please chech the source code and try again"

    )
  169. }

    ,
  170. success:

    function

    (

    resp)

    {
  171. if

    (

    !!

    resp.status

    )

    {
  172. if

    (

    resp.status

    ==

    'success'

    )

    {
  173. var

    _el =

    $(

    '<div>'

    )
  174. _el.hide

    (

    )
  175. _el.addClass

    (

    'alert alert-primary alert_msg'

    )
  176. _el.text

    (

    "Data successfulle updated"

    )

    ;
  177. $(

    '#edit-author-frm'

    )

    .get

    (

    0

    )

    .reset

    (

    )
  178. $(

    '.modal'

    )

    .modal

    (

    'hide'

    )
  179. $(

    '#msg'

    )

    .append

    (

    _el)
  180. _el.show

    (

    'slow'

    )
  181. draw_data(

    )

    ;
  182. setTimeout(

    (

    )

    =>

    {
  183. _el.hide

    (

    'slow'

    )
  184. .remove

    (

    )
  185. }

    ,

    2500

    )
  186. }

    else

    if

    (

    resp.status

    ==

    'success'

    &&

    !!

    resp.msg

    )

    {
  187. var

    _el =

    $(

    '<div>'

    )
  188. _el.hide

    (

    )
  189. _el.addClass

    (

    'alert alert-danger alert_msg form-group'

    )
  190. _el.text

    (

    resp.msg

    )

    ;
  191. $(

    '#edit-author-frm'

    )

    .append

    (

    _el)
  192. _el.show

    (

    'slow'

    )
  193. }

    else

    {
  194. alert(

    "An error occured. Please chech the source code and try again"

    )
  195. }
  196. }

    else

    {
  197. alert(

    "An error occurred. Please check the source code and try again"

    )
  198. }

  199. $(

    '#edit_modal button'

    )

    .attr

    (

    'disabled'

    ,

    false

    )
  200. $(

    '#edit_modal button[form="edit-author-frm"]'

    )

    .text

    (

    "Save"

    )
  201. }
  202. }

    )
  203. }

    )
  204. // DELETE Data
  205. $(

    '#delete-author-frm'

    )

    .submit

    (

    function

    (

    e)

    {
  206. e.preventDefault

    (

    )
  207. $(

    '#delete_modal button'

    )

    .attr

    (

    'disabled'

    ,

    true

    )
  208. $(

    '#delete_modal button[form="delete-author-frm"]'

    )

    .text

    (

    "deleting data ..."

    )
  209. $.ajax

    (

    {
  210. url:

    'delete_data.php'

    ,
  211. data:

    $(

    this

    )

    .serialize

    (

    )

    ,
  212. method:

    'POST'

    ,
  213. dataType:

    "json"

    ,
  214. error:

    err =>

    {
  215. alert(

    "An error occured. Please chech the source code and try again"

    )
  216. }

    ,
  217. success:

    function

    (

    resp)

    {
  218. if

    (

    !!

    resp.status

    )

    {
  219. if

    (

    resp.status

    ==

    'success'

    )

    {
  220. var

    _el =

    $(

    '<div>'

    )
  221. _el.hide

    (

    )
  222. _el.addClass

    (

    'alert alert-primary alert_msg'

    )
  223. _el.text

    (

    "Data successfulle updated"

    )

    ;
  224. $(

    '#delete-author-frm'

    )

    .get

    (

    0

    )

    .reset

    (

    )
  225. $(

    '.modal'

    )

    .modal

    (

    'hide'

    )
  226. $(

    '#msg'

    )

    .append

    (

    _el)
  227. _el.show

    (

    'slow'

    )
  228. draw_data(

    )

    ;
  229. setTimeout(

    (

    )

    =>

    {
  230. _el.hide

    (

    'slow'

    )
  231. .remove

    (

    )
  232. }

    ,

    2500

    )
  233. }

    else

    if

    (

    resp.status

    ==

    'success'

    &&

    !!

    resp.msg

    )

    {
  234. var

    _el =

    $(

    '<div>'

    )
  235. _el.hide

    (

    )
  236. _el.addClass

    (

    'alert alert-danger alert_msg form-group'

    )
  237. _el.text

    (

    resp.msg

    )

    ;
  238. $(

    '#delete-author-frm'

    )

    .append

    (

    _el)
  239. _el.show

    (

    'slow'

    )
  240. }

    else

    {
  241. alert(

    "An error occured. Please chech the source code and try again"

    )
  242. }
  243. }

    else

    {
  244. alert(

    "An error occurred. Please check the source code and try again"

    )
  245. }

  246. $(

    '#delete_modal button'

    )

    .attr

    (

    'disabled'

    ,

    false

    )
  247. $(

    '#delete_modal button[form="delete-author-frm"]'

    )

    .text

    (

    "YEs"

    )
  248. }
  249. }

    )
  250. }

    )
  251. }

    )

    ;

Creating Our PHP API's

The following PHP files are the codes that queries in our database. These are for fetching, adding, and deleting data in oue database. Save the files as the filename above each script.

get_authors.php
  1. <?php
  2. require_once

    (

    "./connect.php"

    )

    ;
  3. extract

    (

    $_POST

    )

    ;

  4. $totalCount

    =

    $conn

    ->

    query

    (

    "SELECT * FROM `authors` "

    )

    ->

    num_rows

    ;
  5. $search_where

    =

    ""

    ;
  6. if

    (

    !

    empty

    (

    $search

    )

    )

    {
  7. $search_where

    =

    " where "

    ;
  8. $search_where

    .=

    " first_name LIKE '%{$search['value']}

    %' "

    ;
  9. $search_where

    .=

    " OR last_name LIKE '%{$search['value']}

    %' "

    ;
  10. $search_where

    .=

    " OR email LIKE '%{$search['value']}

    %' "

    ;
  11. $search_where

    .=

    " OR date_format(birthdate,'%M %d

    , %Y') LIKE '%{$search['value']}

    %' "

    ;
  12. }
  13. $columns_arr

    =

    array

    (

    "id"

    ,
  14. "first_name"

    ,
  15. "last_name"

    ,
  16. "email"

    ,
  17. "unix_timestamp(birthdate)"

    )

    ;
  18. $query

    =

    $conn

    ->

    query

    (

    "SELECT * FROM `authors` {$search_where}

    ORDER BY {$columns_arr

    [$order[0]

    ['column']]} {$order[0]['dir']}

    limit {$length}

    offset {$start}

    "

    )

    ;
  19. $recordsFilterCount

    =

    $conn

    ->

    query

    (

    "SELECT * FROM `authors` {$search_where}

    "

    )

    ->

    num_rows

    ;

  20. $recordsTotal

    =

    $totalCount

    ;
  21. $recordsFiltered

    =

    $recordsFilterCount

    ;
  22. $data

    =

    array

    (

    )

    ;
  23. $i

    =

    1

    +

    $start

    ;
  24. while

    (

    $row

    =

    $query

    ->

    fetch_assoc

    (

    )

    )

    {
  25. $row

    [

    'no'

    ]

    =

    $i

    ++;
  26. $row

    [

    'birthdate'

    ]

    =

    date

    (

    "F d, Y"

    ,

    strtotime

    (

    $row

    [

    'birthdate'

    ]

    )

    )

    ;
  27. $data

    [

    ]

    =

    $row

    ;
  28. }
  29. echo

    json_encode

    (

    array

    (

    'draw'

    =>

    $draw

    ,
  30. 'recordsTotal'

    =>

    $recordsTotal

    ,
  31. 'recordsFiltered'

    =>

    $recordsFiltered

    ,
  32. 'data'

    =>

    $data
  33. )
  34. )

    ;

save_data.php
  1. <?php
  2. require_once

    (

    'connect.php'

    )

    ;
  3. extract

    (

    $_POST

    )

    ;

  4. $query

    =

    $conn

    ->

    query

    (

    "INSERT INTO `authors` (`first_name`,`last_name`,`email`,`birthdate`) VALUE ('{$first_name}

    ','{$last_name}

    ','{$email}

    ','{$birthdate}

    ')"

    )

    ;
  5. if

    (

    $query

    )

    {
  6. $resp

    [

    'status'

    ]

    =

    'success'

    ;
  7. }

    else

    {
  8. $resp

    [

    'status'

    ]

    =

    'failed'

    ;
  9. $resp

    [

    'msg'

    ]

    =

    'An error occured while saving the data. Error: '

    .

    $conn

    ->

    error

    ;
  10. }

  11. echo

    json_encode

    (

    $resp

    )

    ;

get_single.php
  1. <?php
  2. require_once

    (

    'connect.php'

    )

    ;
  3. extract

    (

    $_POST

    )

    ;
  4. $query

    =

    $conn

    ->

    query

    (

    "SELECT * FROM `authors` where id = '{$id}

    '"

    )

    ;
  5. if

    (

    $query

    )

    {
  6. $resp

    [

    'status'

    ]

    =

    'success'

    ;
  7. $resp

    [

    'data'

    ]

    =

    $query

    ->

    fetch_array

    (

    )

    ;
  8. }

    else

    {
  9. $resp

    [

    'status'

    ]

    =

    'success'

    ;
  10. $resp

    [

    'error'

    ]

    =

    'An error occured while fetching the data. Error: '

    .

    $conn

    ->

    error

    ;
  11. }
  12. echo

    json_encode

    (

    $resp

    )

    ;

update_data.php
  1. <?php
  2. require_once

    (

    'connect.php'

    )

    ;
  3. extract

    (

    $_POST

    )

    ;

  4. $update

    =

    $conn

    ->

    query

    (

    "UPDATE `authors` set `first_name` = '{$first_name}

    ', `last_name` = '{$last_name}

    ', `email` = '{$email}

    ',`birthdate` = '{$birthdate}

    ' where id = '{$id}

    '"

    )

    ;
  5. if

    (

    $update

    )

    {
  6. $resp

    [

    'status'

    ]

    =

    'success'

    ;
  7. }

    else

    {
  8. $resp

    [

    'status'

    ]

    =

    'failed'

    ;
  9. $resp

    [

    'msg'

    ]

    =

    'An error occured while saving the data. Error: '

    .

    $conn

    ->

    error

    ;
  10. }

  11. echo

    json_encode

    (

    $resp

    )

    ;

delete_data.php
  1. <?php
  2. require_once

    (

    'connect.php'

    )

    ;
  3. extract

    (

    $_POST

    )

    ;

  4. $delete

    =

    $conn

    ->

    query

    (

    "DELETE FROM `authors` where id = '{$id}

    '"

    )

    ;
  5. if

    (

    $delete

    )

    {
  6. $resp

    [

    'status'

    ]

    =

    'success'

    ;
  7. }

    else

    {
  8. $resp

    [

    'status'

    ]

    =

    'failed'

    ;
  9. $resp

    [

    'msg'

    ]

    =

    'An error occured while saving the data. Error: '

    .

    $conn

    ->

    error

    ;
  10. }

  11. echo

    json_encode

    (

    $resp

    )

    ;

There you go. You can now test the web application that we've created on your end. If there's an error occurred on your end. Please review the scripts above. You can also download the working source code I created. The download button is located below.

DEMO

That's the end of this tutorial. Leave a comment below for any questions. Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding :)


Download
You must upgrade your account or reply in the thread to view hidden text.
 

442,401

317,942

317,951

Top