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

How to Inline Edit Data in Table using PHP and jQuery

Luciddreams

Kernel Vulnerability Auditor
L Rep
0
0
0
Rep
0
L Vouches
0
0
0
Vouches
0
Posts
60
Likes
184
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
This tutorial tackles how to inline edit data in a table derived from MySQL Table. This means that you are editing the table within the table itself and you don't have to be redirected to a certain edit page. We have used jQuery to make this possible and also to handle our ajax request.

Getting jQuery and Bootstrap

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:


Creating our Database

Next, we need to create our MySQL database to handle our data.

I've included a SQL

file in the downloadable of this tutorial. If you have no idea on how to import, please visit my tutorial How import .sql file to restore MySQL database.

To create the database, table and insert sample data using the PHPMyAdmin SQL Tab

. Create a new database in your PHPMyAdmin

naming mydatabase

. Then, copy/paste

the following code in the SQL Tab.

  1. CREATE

    TABLE

    `members`

    (
  2. `id`

    int

    (

    11

    )

    NOT

    NULL

    ,
  3. `firstname`

    varchar

    (

    30

    )

    NOT

    NULL

    ,
  4. `lastname`

    varchar

    (

    30

    )

    NOT

    NULL

    ,
  5. `address`

    text

    NOT

    NULL


  6. )

    ENGINE

    =

    InnoDB

    DEFAULT

    CHARSET

    =

    latin1;

  7. INSERT

    INTO

    `members`

    (

    `id`

    ,

    `firstname`

    ,

    `lastname`

    ,

    `address`

    )

    VALUES


  8. (

    1

    ,

    'neovic'

    ,

    'devierte'

    ,

    'silay city'

    )

    ,
  9. (

    2

    ,

    'gemalyn'

    ,

    'cepe'

    ,

    'carmen, bohol'

    )

    ;


  10. ALTER

    TABLE

    `members`
  11. ADD

    PRIMARY KEY

    (

    `id`

    )

    ;

  12. ALTER

    TABLE

    `members`
  13. MODIFY

    `id`

    int

    (

    11

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,

    AUTO_INCREMENT

    =

    3

    ;

Creating our Interface

Next, we create our table which contains data from our database by creating a new file and name it as index.html

.

  1. <!DOCTYPE html>
  2. <html

    >
  3. <head

    >
  4. <title

    ></

    title

    >
  5. <link

    rel

    =

    "stylesheet"

    type

    =

    "text/css"

    href

    =

    "bootstrap/css/bootstrap.min.css"

    >
  6. <script

    src

    =

    "jquery.min.js"

    ></

    script

    >
  7. </

    head

    >
  8. <body

    >
  9. <div

    class

    =

    "container"

    >
  10. <h1

    class

    =

    "page-header text-center"

    >

    How to Inline Edit using jQuery</

    h1

    >
  11. <div

    class

    =

    "row"

    >
  12. <div

    class

    =

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

    >
  13. <div

    id

    =

    "response"

    class

    =

    "alert text-center"

    style

    =

    "display:none;"

    >
  14. <button

    type

    =

    "button"

    class

    =

    "close"

    id

    =

    "clearMsg"

    ><span

    aria-hidden=

    "true"

    >

    &times;

    </

    span

    ></

    button

    >
  15. <span

    id

    =

    "message"

    ></

    span

    >
  16. </

    div

    >
  17. <table

    class

    =

    "table table-bordered table-striped"

    >
  18. <thead

    >
  19. <tr

    >
  20. <th

    >

    ID</

    th

    >
  21. <th

    >

    Firstname</

    th

    >
  22. <th

    >

    Lastname</

    th

    >
  23. <th

    >

    Address</

    th

    >
  24. <th

    >

    Action</

    th

    >
  25. </

    tr

    >
  26. </

    thead

    >
  27. <tbody

    id

    =

    "tbody"

    >
  28. </

    tbody

    >
  29. </

    table

    >
  30. </

    div

    >
  31. </

    div

    >
  32. </

    div

    >
  33. <script

    src

    =

    "app.js"

    ></

    script

    >
  34. </

    body

    >
  35. </

    html

    >

Creating our jQuery Scripts

Next, we create a javascript file which contains our jQuery scripts and name it as app.js

. This file is included in our index.html

.

  1. $(

    document)

    .ready

    (

    function

    (

    )

    {
  2. //fetch table data
  3. fetch(

    )

    ;
  4. //clicking edit button
  5. $(

    document)

    .on

    (

    'click'

    ,

    '.editbutton'

    ,

    function

    (

    )

    {
  6. var

    row =

    $(

    this

    )

    .closest

    (

    'tr'

    )

    ;
  7. //hide values
  8. row.find

    (

    '.editValue'

    )

    .hide

    (

    )

    ;
  9. //show edit input
  10. row.find

    (

    '.editInput'

    )

    .show

    (

    )

    ;
  11. //show save button
  12. row.find

    (

    '.savebutton'

    )

    .show

    (

    )

    ;
  13. //hide edit button
  14. $(

    this

    )

    .hide

    (

    )

    ;
  15. }

    )

    ;
  16. //save
  17. $(

    document)

    .on

    (

    'click'

    ,

    '.savebutton'

    ,

    function

    (

    )

    {
  18. var

    row =

    $(

    this

    )

    .closest

    (

    'tr'

    )

    ;
  19. //hide textbox
  20. row.find

    (

    '.editInput'

    )

    .hide

    (

    )

    ;
  21. //show value
  22. row.find

    (

    '.editValue'

    )

    .show

    (

    )

    ;
  23. //show edit button
  24. row.find

    (

    '.editbutton'

    )

    .show

    (

    )

    ;
  25. //hide save button
  26. $(

    this

    )

    .hide

    (

    )

    ;
  27. var

    id =

    row.attr

    (

    'id'

    )

    ;
  28. var

    form =

    row.find

    (

    '.editInput'

    )

    .serializeArray

    (

    )

    ;
  29. form.push

    (

    {

    name:

    'id'

    ,

    value:

    id }

    )

    ;
  30. $.ajax

    (

    {
  31. method:

    'POST'

    ,
  32. url:

    'edit.php'

    ,
  33. data:

    form,
  34. dataType:

    'json'

    ,
  35. success:

    function

    (

    response)

    {
  36. if

    (

    response.error

    )

    {
  37. $(

    '#response'

    )

    .show

    (

    )

    .removeClass

    (

    'alert-sucess'

    )

    .addClass

    (

    'alert-danger'

    )

    ;
  38. $(

    '#message'

    )

    .html

    (

    response.message

    )

    ;
  39. }
  40. else

    {
  41. $(

    '#response'

    )

    .show

    (

    )

    .removeClass

    (

    'alert-danger'

    )

    .addClass

    (

    'alert-success'

    )

    ;
  42. $(

    '#message'

    )

    .html

    (

    response.message

    )

    ;

  43. //populate table with updated row
  44. row.find

    (

    '.editValue.firstname'

    )

    .html

    (

    response.member

    .firstname

    )

    ;
  45. row.find

    (

    '.editValue.lastname'

    )

    .html

    (

    response.member

    .lastname

    )

    ;
  46. row.find

    (

    '.editValue.address'

    )

    .html

    (

    response.member

    .address

    )

    ;

  47. row.find

    (

    '.editInput.firstname'

    )

    .val

    (

    response.member

    .firstname

    )

    ;
  48. row.find

    (

    '.editInput.lastname'

    )

    .val

    (

    response.member

    .lastname

    )

    ;
  49. row.find

    (

    '.editInput.address'

    )

    .val

    (

    response.member

    .address

    )

    ;
  50. }
  51. }
  52. }

    )

    ;
  53. }

    )

    ;
  54. //clear msg
  55. $(

    '#clearMsg'

    )

    .click

    (

    function

    (

    )

    {
  56. $(

    '#response'

    )

    .hide

    (

    )

    ;
  57. }

    )

    ;
  58. }

    )

    ;

  59. function

    fetch(

    )

    {
  60. $.ajax

    (

    {
  61. method:

    'GET'

    ,
  62. url:

    'fetch.php'

    ,
  63. success:

    function

    (

    response)

    {
  64. $(

    '#tbody'

    )

    .html

    (

    response)

    ;
  65. }
  66. }

    )

    ;
  67. }

Fetch API of the table

This PHP file contains our codes that fetch table data from our database and appended to our table view.

fetch.php


  1. <?php
  2. $conn

    =

    new

    mysqli(

    'localhost'

    ,

    'root'

    ,

    ''

    ,

    'mydatabase'

    )

    ;

  3. $sql

    =

    "SELECT * FROM members"

    ;
  4. $query

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;
  5. while

    (

    $row

    =

    $query

    ->

    fetch_array

    (

    )

    )

    {
  6. ?>
  7. <tr id="<?php

    echo

    $row

    [

    'id'

    ]

    ;

    ?>

    ">
  8. <td>
  9. <?php

    echo

    $row

    [

    'id'

    ]

    ;

    ?>
  10. </td>
  11. <td>
  12. <span class="editValue firstname"><?php

    echo

    $row

    [

    'firstname'

    ]

    ;

    ?>

    </span>
  13. <input type="text" class="form-control editInput firstname" name="firstname" value="<?php

    echo

    $row

    [

    'firstname'

    ]

    ;

    ?>

    " style="display:none;">
  14. </td>
  15. <td>
  16. <span class="editValue lastname"><?php

    echo

    $row

    [

    'lastname'

    ]

    ;

    ?>

    </span>
  17. <input type="text" class="form-control editInput lastname" name="lastname" value="<?php

    echo

    $row

    [

    'lastname'

    ]

    ;

    ?>

    " style="display:none;">
  18. </td>
  19. <td>
  20. <span class="editValue address"><?php

    echo

    $row

    [

    'address'

    ]

    ;

    ?>

    </span>
  21. <input type="text" class="form-control editInput address" name="address" value="<?php

    echo

    $row

    [

    'address'

    ]

    ;

    ?>

    " style="display:none;">
  22. </td>
  23. <td>
  24. <button class="btn btn-primary editbutton"><span class="glyphicon glyphicon-edit"></span> Edit</button>
  25. <button class="btn btn-success savebutton" style="display:none;"><span class="glyphicon glyphicon-floppy-disk"></span> Save</button>
  26. </td>
  27. </tr>
  28. <?php
  29. }



  30. ?>

Update Script

Lastly, this is our PHP code that saves the update made to our table view.

edit.php


  1. <?php
  2. $conn

    =

    new

    mysqli(

    'localhost'

    ,

    'root'

    ,

    ''

    ,

    'mydatabase'

    )

    ;

  3. $output

    =

    array

    (

    'error'

    =>

    false

    )

    ;

  4. $id

    =

    $_POST

    [

    'id'

    ]

    ;
  5. $firstname

    =

    $_POST

    [

    'firstname'

    ]

    ;
  6. $lastname

    =

    $_POST

    [

    'lastname'

    ]

    ;
  7. $address

    =

    $_POST

    [

    'address'

    ]

    ;

  8. $sql

    =

    "UPDATE members SET firstname = '$firstname

    ', lastname = '$lastname

    ', address = '$address

    ' WHERE id = '$id

    '"

    ;
  9. $query

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;

  10. if

    (

    $query

    )

    {
  11. $output

    [

    'message'

    ]

    =

    'Member updated successfully'

    ;
  12. //return the updated member
  13. $output

    [

    'member'

    ]

    =

    array

    (
  14. 'firstname'

    =>

    $firstname

    ,
  15. 'lastname'

    =>

    $lastname

    ,
  16. 'address'

    =>

    $address
  17. )

    ;
  18. }
  19. else

    {
  20. $output

    [

    'error'

    ]

    =

    true

    ;
  21. $output

    [

    'message'

    ]

    =

    'Cannot update member'

    ;
  22. }

  23. echo

    json_encode

    (

    $output

    )

    ;

  24. ?>

That ends this tutorial. I hope this tutorial will help you with what your are looking for and you'll find this useful for your future PHP Projects.

Happy Coding :)


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

452,292

324,186

324,194

Top