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

Inline Table Row Insert, Update, and Delete using PHP and jQuery Tutorial

HassanAdnan

DeFi Pro
H Rep
0
0
0
Rep
0
H Vouches
0
0
0
Vouches
0
Posts
91
Likes
186
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial, you wil learn how to create an Inline Table Row Insert, Update, and Delete using PHP/OOP, MySQL Database,and JavaScript (jQuery adn Ajax). Our main goal for this tutorial is to display data from database to HTML table that can add, editm remove table row dynamically. The table cells in the table will be editable if the user add or edit a row. The source code must have a field validator for required fields and field text types.

Getting Started

I'll be using Bootstrap and jQuery plugins/libraries on this tutorial. And to run my PHP Script, I will use XAMPP version 3.3.0 which has a PHP version 8.0.7.

Before we continue, please make sure that your XAMPP/WAMP's Apache and MySQL are already started. To do this, open your XAMPP/WAMP's Control Panel and start the mentioned web and database server.

Creating the Database



Open the PHPMyAdmin in a browser i.e. http://localhost/phpmyadmin

and create new dayabase naming inline_db

. After that, navigate the page to the SQL Tab and copy/paste

the sql script below.

  1. CREATE

    TABLE

    `members`

    (
  2. `id`

    int

    (

    30

    )

    NOT

    NULL

    AUTO_INCREMENT

    PRIMARY KEY

    ,
  3. `name`

    text

    NOT

    NULL

    ,
  4. `contact`

    varchar

    (

    50

    )

    NOT

    NULL

    ,
  5. `email`

    varchar

    (

    250

    )

    NOT

    NULL

    ,
  6. `address`

    text

    NOT

    NULL


  7. )

    ENGINE

    =

    InnoDB

    DEFAULT

    CHARSET

    =

    utf8mb4;

Creating the Database Connection



Create a new PHP File and save it as database.php

. Then, copy/paste

the folling PHP script below.

  1. <?php
  2. class

    DBConnection{

  3. private

    $host

    =

    'localhost'

    ;
  4. private

    $username

    =

    'root'

    ;
  5. private

    $password

    =

    ''

    ;
  6. private

    $database

    =

    'inline_db'

    ;

  7. public

    $conn

    ;

  8. public

    function

    __construct(

    )

    {

  9. if

    (

    !

    isset

    (

    $this

    ->

    conn

    )

    )

    {

  10. $this

    ->

    conn

    =

    new

    mysqli(

    $this

    ->

    host

    ,

    $this

    ->

    username

    ,

    $this

    ->

    password

    ,

    $this

    ->

    database

    )

    ;

  11. if

    (

    !

    $this

    ->

    conn

    )

    {
  12. echo

    'Cannot connect to database server'

    ;
  13. exit

    ;
  14. }
  15. }

  16. }
  17. public

    function

    __destruct(

    )

    {
  18. $this

    ->

    conn

    ->

    close

    (

    )

    ;
  19. }
  20. }
  21. ?>

Creating Our Interface



The script below is the code for the index file of the web application. Save the following script as index.php

.

Note: please configure the external CSS and Javascript File Script according to your assets directory.

  1. <?php
  2. //Including the Database Connection Class
  3. require_once

    (

    'database.php'

    )

    ;
  4. $db

    =

    new

    DBConnection(

    )

    ;
  5. $conn

    =

    $db

    ->

    conn

    ;
  6. ?>
  7. <!DOCTYPE html>
  8. <html lang="en">
  9. <head>
  10. <meta charset="UTF-8">
  11. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  12. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  13. <title>Inline Table Form</title>
  14. <link rel="stylesheet" href="./assets/css/bootstrap.css">
  15. <style>
  16. .editable{
  17. display:none;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
  23. <div class="container-fluid">
  24. <a class="navbar-brand" href="https://sourcecodester.com">SourceCodester</a>
  25. </div>
  26. </nav>
  27. <div class="container py-3">
  28. <h2 class="border-bottom border-dark">Table Inline Form</h2>
  29. <span>Inline Table row cells adding and editing data using PHP and jQuery</span>

  30. <div class="row">
  31. <div class="col-12">
  32. <h3 class="text-center">Member List</h3>
  33. </div>
  34. <hr>
  35. <div class="col-12">
  36. <!-- Table Form start -->
  37. <form action="" id="form-data">
  38. <input type="hidden" name="id" value="">
  39. <table class='table table-hovered table-stripped table-bordered' id="form-tbl">
  40. <colgroup>
  41. <col width="20%">
  42. <col width="25%">
  43. <col width="15%">
  44. <col width="25%">
  45. <col width="15%">
  46. </colgroup>
  47. <thead>
  48. <tr>
  49. <th class="text-center p-1">Name</th>
  50. <th class="text-center p-1">Email</th>
  51. <th class="text-center p-1">Contact</th>
  52. <th class="text-center p-1">Address</th>
  53. <th class="text-center p-1">Action</th>
  54. </tr>
  55. </thead>
  56. <tbody>
  57. <?php
  58. $query

    =

    $conn

    ->

    query

    (

    "SELECT * FROM `members` order by id asc"

    )

    ;
  59. while

    (

    $row

    =

    $query

    ->

    fetch_assoc

    (

    )

    )

    :
  60. ?>
  61. <tr data-id='<?php

    echo

    $row

    [

    'id'

    ]

    ?>

    '>
  62. <td name="name"><?php

    echo

    $row

    [

    'name'

    ]

    ?>

    </td>
  63. <td name="email"><?php

    echo

    $row

    [

    'email'

    ]

    ?>

    </td>
  64. <td name="contact"><?php

    echo

    $row

    [

    'contact'

    ]

    ?>

    </td>
  65. <td name="address"><?php

    echo

    $row

    [

    'address'

    ]

    ?>

    </td>
  66. <td class="text-center">
  67. <button class="btn btn-primary btn-sm rounded-0 py-0 edit_data noneditable" type="button">Edit</button>
  68. <button class="btn btn-danger btn-sm rounded-0 py-0 delete_data noneditable" type="button">Delete</button>
  69. <button class="btn btn-sm btn-primary btn-flat rounded-0 px-2 py-0 editable">Save</button>
  70. <button class="btn btn-sm btn-dark btn-flat rounded-0 px-2 py-0 editable" onclick="cancel_button($(this))" type="button">Cancel</button></td>
  71. </td>
  72. </tr>
  73. <?php

    endwhile

    ;

    ?>
  74. </tbody>
  75. </table>
  76. </form>
  77. <!-- Table Form end -->
  78. </div>
  79. <div class="w-100 d-flex pposition-relative justify-content-center">
  80. <button class="btn btn-flat btn-primary" id="add_member" type="button">Add New Member</button>
  81. </div>
  82. </div>
  83. </div>
  84. </body>
  85. <script type="text/javascript" src="./assets/js/jquery-3.6.0.js"></script>
  86. <script type="text/javascript" src="./assets/js/bootstrap.js"></script>
  87. <!-- This is the javascript file that contains the actions scripts of the table -->
  88. <script type="text/javascript" src="./assets/js/script.js"></script>
  89. </html>

Creating the JavaScript File



In this script, it contains the functionalities of the buttons and the Ajax Request scripts for the form submission. Save the following script as script.js



  1. $(

    function

    (

    )

    {
  2. // Create New Row
  3. $(

    '#add_member'

    )

    .click

    (

    function

    (

    )

    {
  4. if

    (

    $(

    'tr[data-id=""]'

    )

    .length

    >

    0

    )

    {
  5. $(

    'tr[data-id=""]'

    )

    .find

    (

    '[name="name"]'

    )

    .focus

    (

    )
  6. return

    false

    ;
  7. }
  8. var

    tr =

    $(

    '<tr>'

    )
  9. $(

    'input[name="id"]'

    )

    .val

    (

    ''

    )
  10. tr.addClass

    (

    'py-1 px-2'

    )

    ;
  11. tr.attr

    (

    'data-id'

    ,

    ''

    )

    ;
  12. tr.append

    (

    '<td contenteditable name="name"></td>'

    )
  13. tr.append

    (

    '<td contenteditable name="email"></td>'

    )
  14. tr.append

    (

    '<td contenteditable name="contact"></td>'

    )
  15. tr.append

    (

    '<td contenteditable name="address"></td>'

    )
  16. tr.append

    (

    '<td class="text-center"><button class="btn btn-sm btn-primary btn-flat rounded-0 px-2 py-0">Save</button><button class="btn btn-sm btn-dark btn-flat rounded-0 px-2 py-0" onclick="cancel_button($(this))" type="button">Cancel</button></td>'

    )
  17. $(

    '#form-tbl'

    )

    .append

    (

    tr)
  18. tr.find

    (

    '[name="name"]'

    )

    .focus

    (

    )
  19. }

    )

  20. // Edit Row
  21. $(

    '.edit_data'

    )

    .click

    (

    function

    (

    )

    {
  22. var

    id =

    $(

    this

    )

    .closest

    (

    'tr'

    )

    .attr

    (

    'data-id'

    )
  23. $(

    'input[name="id"]'

    )

    .val

    (

    id)
  24. var

    count_column =

    $(

    this

    )

    .closest

    (

    'tr'

    )

    .find

    (

    'td'

    )

    .length
  25. $(

    this

    )

    .closest

    (

    'tr'

    )

    .find

    (

    'td'

    )

    .each

    (

    function

    (

    )

    {
  26. if

    (

    $(

    this

    )

    .index

    (

    )

    !=

    (

    count_column -

    1

    )

    )
  27. $(

    this

    )

    .attr

    (

    'contenteditable'

    ,

    true

    )
  28. }

    )
  29. $(

    this

    )

    .closest

    (

    'tr'

    )

    .find

    (

    '[name="name"]'

    )

    .focus

    (

    )
  30. $(

    this

    )

    .closest

    (

    'tr'

    )

    .find

    (

    '.editable'

    )

    .show

    (

    'fast'

    )
  31. $(

    this

    )

    .closest

    (

    'tr'

    )

    .find

    (

    '.noneditable'

    )

    .hide

    (

    'fast'

    )
  32. }

    )

  33. // Delete Row
  34. $(

    '.delete_data'

    )

    .click

    (

    function

    (

    )

    {
  35. var

    id =

    $(

    this

    )

    .closest

    (

    'tr'

    )

    .attr

    (

    'data-id'

    )
  36. var

    name =

    $(

    this

    )

    .closest

    (

    'tr'

    )

    .find

    (

    "[name='name']"

    )

    .text

    (

    )
  37. var

    _conf =

    confirm(

    "Are you sure to delete \"

    "

    +

    name +

    "\"

    from the list?"

    )
  38. if

    (

    _conf ==

    true

    )

    {
  39. $.ajax

    (

    {
  40. url:

    'api.php?action=delete'

    ,
  41. method:

    'POST'

    ,
  42. data:

    {

    id:

    id }

    ,
  43. dataType:

    'json'

    ,
  44. error:

    err =>

    {
  45. alert(

    "An error occured while saving the data"

    )
  46. console.log

    (

    err)
  47. }

    ,
  48. success:

    function

    (

    resp)

    {
  49. if

    (

    resp.status

    ==

    'success'

    )

    {
  50. alert(

    name +

    ' is successfully deleted from the list.'

    )
  51. location.reload

    (

    )
  52. }

    else

    {
  53. alert(

    resp.msg

    )
  54. console.log

    (

    err)
  55. }
  56. }
  57. }

    )
  58. }
  59. }

    )

  60. $(

    '#form-data'

    )

    .submit

    (

    function

    (

    e)

    {
  61. e.preventDefault

    (

    )

    ;
  62. var

    id =

    $(

    'input[name="id"]'

    )

    .val

    (

    )
  63. var

    data =

    {

    }

    ;
  64. // check fields promise
  65. var

    check_fields =

    new

    Promise(

    function

    (

    resolve,

    reject)

    {
  66. data[

    'id'

    ]

    =

    id;
  67. $(

    'td[contenteditable]'

    )

    .each

    (

    function

    (

    )

    {
  68. data[

    $(

    this

    )

    .attr

    (

    'name'

    )

    ]

    =

    $(

    this

    )

    .text

    (

    )
  69. if

    (

    data[

    $(

    this

    )

    .attr

    (

    'name'

    )

    ]

    ==

    ''

    )

    {
  70. alert(

    "All fields are required."

    )

    ;
  71. resolve(

    false

    )

    ;
  72. return

    false

    ;
  73. }
  74. }

    )
  75. resolve(

    true

    )

    ;
  76. }

    )
  77. // continue only if all fields are filled
  78. check_fields.then

    (

    function

    (

    resp)

    {
  79. if

    (

    !

    resp)
  80. return

    false

    ;
  81. // validate email
  82. if

    (

    !

    IsEmail(

    data[

    'email'

    ]

    )

    )

    {
  83. alert(

    "Invalid Email."

    )

    ;
  84. $(

    '[name="email"][contenteditable]'

    )

    .addClass

    (

    'bg-danger text-light bg-opacity-50'

    )

    .focus

    (

    )

    ;
  85. return

    false

    ;
  86. }

    else

    {
  87. $(

    '[name="email"][contenteditable]'

    )

    .removeClass

    (

    'bg-danger text-light bg-opacity-50'

    )
  88. }

  89. // validate contact #
  90. if

    (

    !

    isContact(

    data[

    'contact'

    ]

    )

    )

    {
  91. alert(

    "Invalid Contact Number."

    )

    ;
  92. $(

    '[name="contact"][contenteditable]'

    )

    .addClass

    (

    'bg-danger text-light bg-opacity-50'

    )

    .focus

    (

    )

    ;
  93. return

    false

    ;
  94. }

    else

    {
  95. $(

    '[name="contact"][contenteditable]'

    )

    .removeClass

    (

    'bg-danger text-light bg-opacity-50'

    )
  96. }
  97. $.ajax

    (

    {
  98. url:

    "./api.php?action=save"

    ,
  99. method:

    'POST'

    ,
  100. data:

    data,
  101. dataType:

    'json'

    ,
  102. error:

    err =>

    {
  103. alert(

    'An error occured while saving the data'

    )

    ;
  104. console.log

    (

    err)
  105. }

    ,
  106. success:

    function

    (

    resp)

    {
  107. if

    (

    !!

    resp.status

    &&

    resp.status

    ==

    'success'

    )

    {
  108. alert(

    resp.msg

    )

    ;
  109. location.reload

    (

    )
  110. }

    else

    {
  111. alert(

    resp.msg

    )

    ;
  112. }
  113. }
  114. }

    )
  115. }

    )


  116. }

    )
  117. }

    )
  118. //Email Validation Function
  119. window.IsEmail

    =

    function

    (

    email)

    {
  120. var

    regex =

    /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/

    ;
  121. if

    (

    !

    regex.test

    (

    email)

    )

    {
  122. return

    false

    ;
  123. }

    else

    {
  124. return

    true

    ;
  125. }
  126. }
  127. //Contact Number Validation Function
  128. window.isContact

    =

    function

    (

    contact)

    {
  129. return

    (

    $.isNumeric

    (

    contact)

    &&

    contact.length

    ==

    11

    &&

    contact.substr

    (

    0

    ,

    2

    )

    ==

    '09'

    )

    ;
  130. }

  131. // removing table row when cancel button triggered clicked
  132. window.cancel_button

    =

    function

    (

    _this)

    {
  133. if

    (

    _this.closest

    (

    'tr'

    )

    .attr

    (

    'data-id'

    )

    ==

    ''

    )

    {
  134. _this.closest

    (

    'tr'

    )

    .remove

    (

    )
  135. }

    else

    {
  136. $(

    'input[name="id"]'

    )

    .val

    (

    ''

    )
  137. _this.closest

    (

    'tr'

    )

    .find

    (

    'td'

    )

    .each

    (

    function

    (

    )

    {
  138. $(

    this

    )

    .removeAttr

    (

    'contenteditable'

    )
  139. }

    )
  140. _this.closest

    (

    'tr'

    )

    .find

    (

    '.editable'

    )

    .hide

    (

    'fast'

    )
  141. _this.closest

    (

    'tr'

    )

    .find

    (

    '.noneditable'

    )

    .show

    (

    'fast'

    )
  142. }
  143. }

Creating Our PHP API



Lastly, we will create the PHP File that contains our API Queries for Insertinh, Updating, and Deleting the data in the database. Save the file as api.php

.

  1. <?php
  2. require_once

    (

    'database.php'

    )

    ;
  3. Class

    API extends

    DBConnection{
  4. public

    function

    __construct(

    )

    {
  5. parent::

    __construct(

    )

    ;
  6. }
  7. public

    function

    __destruct(

    )

    {
  8. parent::

    __destruct(

    )

    ;
  9. }

  10. function

    save_member(

    )

    {
  11. $data

    =

    ""

    ;
  12. $id

    =

    $_POST

    [

    'id'

    ]

    ;
  13. foreach

    (

    $_POST

    as

    $k

    =>

    $v

    )

    {
  14. // excluding id
  15. if

    (

    !

    in_array

    (

    $k

    ,

    array

    (

    'id'

    )

    )

    )

    {
  16. // add comma if data variable is not empty
  17. if

    (

    !

    empty

    (

    $data

    )

    )

    $data

    .=

    ", "

    ;
  18. $data

    .=

    " `{$k}

    ` = '{$v}

    ' "

    ;
  19. }
  20. }
  21. if

    (

    empty

    (

    $id

    )

    )

    {
  22. // Insert New Member
  23. $sql

    =

    "INSERT INTO `members` set {$data}

    "

    ;
  24. }

    else

    {
  25. // Update Member's Details
  26. $sql

    =

    "UPDATE `members` set {$data}

    where id = '{$id}

    '"

    ;
  27. }
  28. $save

    =

    $this

    ->

    conn

    ->

    query

    (

    $sql

    )

    ;
  29. if

    (

    $save

    &&

    !

    $this

    ->

    conn

    ->

    error

    )

    {
  30. $resp

    [

    'status'

    ]

    =

    'success'

    ;
  31. if

    (

    empty

    (

    $id

    )

    )
  32. $resp

    [

    'msg'

    ]

    =

    'New Member successfully added'

    ;
  33. else
  34. $resp

    [

    'msg'

    ]

    =

    'Member\'s Details successfully updated'

    ;

  35. }

    else

    {
  36. $resp

    [

    'status'

    ]

    =

    'failed'

    ;
  37. $resp

    [

    'msg'

    ]

    =

    'There\'s an error occured while saving the data'

    ;
  38. $resp

    [

    'error'

    ]

    =

    $this

    ->

    conn

    ->

    error

    ;
  39. }
  40. return

    json_encode

    (

    $resp

    )

    ;
  41. }
  42. function

    delete_member(

    )

    {
  43. $id

    =

    $_POST

    [

    'id'

    ]

    ;
  44. // Delete Query
  45. $delete

    =

    $this

    ->

    conn

    ->

    query

    (

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

    '"

    )

    ;
  46. if

    (

    $delete

    )

    {
  47. $resp

    [

    'status'

    ]

    =

    'success'

    ;
  48. }

    else

    {
  49. $resp

    [

    'status'

    ]

    =

    'failed'

    ;
  50. $resp

    [

    'msg'

    ]

    =

    'There\'s an error occured while deleting the data'

    ;
  51. $resp

    [

    'error'

    ]

    =

    $this

    ->

    conn

    ->

    error

    ;
  52. }
  53. return

    json_encode

    (

    $resp

    )

    ;
  54. }

  55. }

  56. $action

    =

    isset

    (

    $_GET

    [

    'action'

    ]

    )

    ? $_GET

    [

    'action'

    ]

    :

    ''

    ;
  57. $api

    =

    new

    API(

    )

    ;
  58. switch

    (

    $action

    )

    {
  59. case

    (

    'save'

    )

    :
  60. echo

    $api

    ->

    save_member

    (

    )

    ;
  61. break

    ;
  62. case

    (

    'delete'

    )

    :
  63. echo

    $api

    ->

    delete_member

    (

    )

    ;
  64. break

    ;
  65. default

    :
  66. echo

    json_encode

    (

    array

    (

    'status'

    =>

    'failed'

    ,

    'error'

    =>

    'unknown action'

    )

    )

    ;
  67. break

    ;

  68. }
  69. ?>

DEMO

There you go! You can now test your work at your end and if an error occurred please review the scripts above. You can also download the working source code that I have created for this tutorial. Just simply click the Download Button below to download the provided source code zip file.

I hope this tutorial will help you with what you are looking for and you'll find this useful for your future web app projects using PHP and jQuery. Explore more on this website for more Free Source Codes and Tutorials.

Happy Coding :)


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

452,292

323,526

323,535

Top