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

PHP - OOP CRUD Operation using AJAX/jQuery

babatundebaabs

Stand-Up Strategist
Divine
B Rep
0
0
0
Rep
0
B Vouches
0
0
0
Vouches
0
Posts
127
Likes
49
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
Getting Started

Please take note that CSS and jQuery used in this tutorial are hosted so you need internet connection for them to work.

Creating our Database

First step is to create our database.
1. Open phpMyAdmin.
2. Click databases, create a database and name it as crud_oop.
3. After creating a database, click the SQL and paste the below codes. See image below for detailed instruction.

  1. CREATE

    TABLE

    `member`

    (
  2. `memberid`

    INT

    (

    11

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,
  3. `firstname`

    VARCHAR

    (

    50

    )

    NOT

    NULL

    ,
  4. `lastname`

    VARCHAR

    (

    50

    )

    NOT

    NULL

    ,
  5. PRIMARY

    KEY

    (

    `memberid`

    )
  6. )

    ENGINE=

    InnoDB DEFAULT

    CHARSET=

    latin1;

database_6_12.png

Creating our Connection

Next, we create our connection to our database. This will serve as the bridge between our forms and database. We name this as conn.php.

  1. <?php

  2. $conn

    =

    new

    mysqli(

    "localhost"

    ,

    "root"

    ,

    ""

    ,

    "crud_oop"

    )

    ;

  3. if

    (

    $conn

    ->

    connect_error

    )

    {
  4. die

    (

    "Connection failed: "

    .

    $conn

    ->

    connect_error

    )

    ;
  5. }

  6. ?>

index.php

We create our index that contains our table.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>PHP - OOP CRUD Operation using AJAX/jQuery</title>
  5. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  6. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  7. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  8. </head>
  9. <body>
  10. <body>
  11. <div class="container">
  12. <div style="height:50px;"></div>
  13. <div class="well" style="margin-left:auto; margin-right:auto; padding:auto; width:70%;">
  14. <span style="font-size:25px; color:blue"><strong>PHP - OOP CRUD Operation using AJAX/jQuery</strong></span>
  15. <span class="pull-right"><a id="add" style="cursor:pointer;" class="btn btn-primary"><span class="glyphicon glyphicon-plus"></span> Add New</a></span>
  16. <div style="height:15px;"></div>
  17. <div id="table"></div>
  18. <div id="alert" class="alert alert-success" style="display:none;">
  19. <center><span id="alerttext"></span></center>
  20. </div>
  21. </div>
  22. <?php

    include

    (

    'modal.php'

    )

    ;

    ?>
  23. <script src="custom.js"></script>
  24. </div>
  25. </body>
  26. </html>

custom.js

This contains our AJAX and jQuery codes.

  1. $(

    document)

    .ready

    (

    function

    (

    )

    {
  2. showTable(

    )

    ;

  3. //add
  4. $(

    '#add'

    )

    .click

    (

    function

    (

    )

    {
  5. $(

    '#addnew'

    )

    .modal

    (

    'show'

    )

    ;
  6. $(

    '#addForm'

    )

    [

    0

    ]

    .reset

    (

    )

    ;
  7. }

    )

    ;

  8. $(

    '#addbutton'

    )

    .click

    (

    function

    (

    )

    {
  9. var

    first =

    $(

    '#firstname'

    )

    .val

    (

    )

    ;
  10. var

    last =

    $(

    '#lastname'

    )

    .val

    (

    )

    ;
  11. if

    (

    first!=

    ''

    &&

    last!==

    ''

    )

    {
  12. var

    addForm =

    $(

    '#addForm'

    )

    .serialize

    (

    )

    ;
  13. $.ajax

    (

    {
  14. type:

    'POST'

    ,
  15. url:

    'add.php'

    ,
  16. data:

    addForm,
  17. success:

    function

    (

    )

    {
  18. $(

    '#addnew'

    )

    .modal

    (

    'hide'

    )

    ;
  19. $(

    '#addForm'

    )

    [

    0

    ]

    .reset

    (

    )

    ;
  20. showTable(

    )

    ;
  21. $(

    '#alert'

    )

    .slideDown

    (

    )

    ;
  22. $(

    '#alerttext'

    )

    .text

    (

    'Member Added Successfully'

    )

    ;
  23. }
  24. }

    )

    ;
  25. }
  26. else

    {
  27. alert(

    'Please input both Fields'

    )
  28. }

  29. }

    )

    ;
  30. //
  31. //edit
  32. $(

    document)

    .on

    (

    'click'

    ,

    '.edit'

    ,

    function

    (

    )

    {
  33. var

    memid =

    $(

    this

    )

    .data

    (

    'id'

    )

    ;
  34. var

    first =

    $(

    '#first'

    +

    memid)

    .text

    (

    )

    ;
  35. var

    last =

    $(

    '#last'

    +

    memid)

    .text

    (

    )

    ;
  36. $(

    '#editmem'

    )

    .modal

    (

    'show'

    )

    ;
  37. $(

    '#efirstname'

    )

    .val

    (

    first)

    ;
  38. $(

    '#elastname'

    )

    .val

    (

    last)

    ;
  39. $(

    '#editbutton'

    )

    .val

    (

    memid)

    ;
  40. }

    )

    ;

  41. $(

    '#editbutton'

    )

    .click

    (

    function

    (

    )

    {
  42. var

    memid =

    $(

    this

    )

    .val

    (

    )

    ;
  43. var

    editForm =

    $(

    '#editForm'

    )

    .serialize

    (

    )

    ;
  44. $.ajax

    (

    {
  45. type:

    'POST'

    ,
  46. url:

    'edit.php'

    ,
  47. data:

    editForm +

    "&memid="

    +

    memid,
  48. success:

    function

    (

    )

    {
  49. $(

    '#editmem'

    )

    .modal

    (

    'hide'

    )

    ;
  50. $(

    '#editForm'

    )

    [

    0

    ]

    .reset

    (

    )

    ;
  51. showTable(

    )

    ;
  52. $(

    '#alert'

    )

    .slideDown

    (

    )

    ;
  53. $(

    '#alerttext'

    )

    .text

    (

    'Member Updated Successfully'

    )

    ;
  54. }
  55. }

    )

    ;
  56. }

    )

    ;
  57. //
  58. //delete
  59. $(

    document)

    .on

    (

    'click'

    ,

    '.delete'

    ,

    function

    (

    )

    {
  60. var

    memid =

    $(

    this

    )

    .data

    (

    'id'

    )

    ;
  61. var

    first =

    $(

    '#first'

    +

    memid)

    .text

    (

    )

    ;
  62. $(

    '#delmem'

    )

    .modal

    (

    'show'

    )

    ;
  63. $(

    '#dfirstname'

    )

    .text

    (

    first)

    ;
  64. $(

    '#delbutton'

    )

    .val

    (

    memid)

    ;
  65. }

    )

    ;

  66. $(

    '#delbutton'

    )

    .click

    (

    function

    (

    )

    {
  67. var

    memid =

    $(

    this

    )

    .val

    (

    )

    ;
  68. $.ajax

    (

    {
  69. type:

    'POST'

    ,
  70. url:

    'delete.php'

    ,
  71. data:

    {
  72. memid:

    memid,
  73. }

    ,
  74. success:

    function

    (

    )

    {
  75. $(

    '#delmem'

    )

    .modal

    (

    'hide'

    )

    ;
  76. showTable(

    )

    ;
  77. $(

    '#alert'

    )

    .slideDown

    (

    )

    ;
  78. $(

    '#alerttext'

    )

    .text

    (

    'Member Deleted Successfully'

    )

    ;
  79. }
  80. }

    )

    ;
  81. }

    )

    ;

  82. }

    )

    ;

  83. function

    showTable(

    )

    {
  84. $.ajax

    (

    {
  85. type:

    'POST'

    ,
  86. url:

    'fetch.php'

    ,
  87. data:

    {
  88. fetch:

    1
  89. }

    ,
  90. success:

    function

    (

    data)

    {
  91. $(

    '#table'

    )

    .html

    (

    data)

    ;
  92. }
  93. }

    )

    ;
  94. }

fetch.php

This is our code in fetching table data from our database.

  1. <?php
  2. include

    (

    'conn.php'

    )

    ;
  3. if

    (

    isset

    (

    $_POST

    [

    'fetch'

    ]

    )

    )

    {
  4. ?>
  5. <table class="table table-striped table-bordered table-hover">
  6. <thead>
  7. <th>Firstname</th>
  8. <th>Lastname</th>
  9. <th>Action</th>
  10. </thead>
  11. <tbody>
  12. <?php
  13. $query

    =

    $conn

    ->

    query

    (

    "select * from `member`"

    )

    ;
  14. while

    (

    $row

    =

    $query

    ->

    fetch_array

    (

    )

    )

    {
  15. ?>
  16. <tr>
  17. <td><span id="first<?php

    echo

    $row

    [

    'memberid'

    ]

    ;

    ?>

    "><?php

    echo

    $row

    [

    'firstname'

    ]

    ;

    ?>

    </span></td>
  18. <td><span id="last<?php

    echo

    $row

    [

    'memberid'

    ]

    ;

    ?>

    "><?php

    echo

    $row

    [

    'lastname'

    ]

    ;

    ?>

    </span></td>
  19. <td>
  20. <a style="cursor:pointer;" class="btn btn-warning edit" data-id="<?php

    echo

    $row

    [

    'memberid'

    ]

    ;

    ?>

    "><span class="glyphicon glyphicon-edit"></span> Edit</a> ||
  21. <a style="cursor:pointer;" class="btn btn-danger delete" data-id="<?php

    echo

    $row

    [

    'memberid'

    ]

    ;

    ?>

    "><span class="glyphicon glyphicon-trash"></span> Delete</a>
  22. </td>
  23. </tr>
  24. <?php
  25. }

  26. ?>
  27. </tbody>
  28. </table>
  29. <?php
  30. }
  31. ?>

modal.php

This contains our add, edit and delete modal.

  1. <!-- Add New -->
  2. <div

    class

    =

    "modal fade"

    id

    =

    "addnew"

    tabindex

    =

    "-1"

    role=

    "dialog"

    aria-labelledby=

    "myModalLabel"

    aria-hidden=

    "true"

    >
  3. <div

    class

    =

    "modal-dialog"

    >
  4. <div

    class

    =

    "modal-content"

    >
  5. <div

    class

    =

    "modal-header"

    >
  6. <button

    type

    =

    "button"

    class

    =

    "close"

    data-dismiss=

    "modal"

    aria-hidden=

    "true"

    >

    &times;

    </

    button

    >
  7. <center

    ><h4

    class

    =

    "modal-title"

    id

    =

    "myModalLabel"

    >

    Add New Member</

    h4

    ></

    center

    >
  8. </

    div

    >
  9. <div

    class

    =

    "modal-body"

    >
  10. <div

    class

    =

    "container-fluid"

    >
  11. <form

    id

    =

    "addForm"

    >
  12. <div

    class

    =

    "row"

    >
  13. <div

    class

    =

    "col-md-2"

    >
  14. <label

    class

    =

    "control-label"

    style

    =

    "position:relative; top:7px;"

    >

    Firstname:</

    label

    >
  15. </

    div

    >
  16. <div

    class

    =

    "col-md-10"

    >
  17. <input

    type

    =

    "text"

    class

    =

    "form-control"

    name

    =

    "firstname"

    id

    =

    "firstname"

    >
  18. </

    div

    >
  19. </

    div

    >
  20. <div

    style

    =

    "height:10px;"

    ></

    div

    >
  21. <div

    class

    =

    "row"

    >
  22. <div

    class

    =

    "col-md-2"

    >
  23. <label

    class

    =

    "control-label"

    style

    =

    "position:relative; top:7px;"

    >

    Lastname:</

    label

    >
  24. </

    div

    >
  25. <div

    class

    =

    "col-md-10"

    >
  26. <input

    type

    =

    "text"

    class

    =

    "form-control"

    name

    =

    "lastname"

    id

    =

    "lastname"

    >
  27. </

    div

    >
  28. </

    div

    >
  29. </

    div

    >
  30. </

    div

    >
  31. <div

    class

    =

    "modal-footer"

    >
  32. <button

    type

    =

    "button"

    class

    =

    "btn btn-default"

    data-dismiss=

    "modal"

    ><span

    class

    =

    "glyphicon glyphicon-remove"

    ></

    span

    >

    Cancel</

    button

    >
  33. <button

    type

    =

    "button"

    id

    =

    "addbutton"

    class

    =

    "btn btn-primary"

    ><span

    class

    =

    "glyphicon glyphicon-floppy-disk"

    ></

    span

    >

    Save</

    a

    >
  34. </

    form

    >
  35. </

    div

    >

  36. </

    div

    >
  37. </

    div

    >
  38. </

    div

    >

  39. <!-- Edit -->
  40. <div

    class

    =

    "modal fade"

    id

    =

    "editmem"

    tabindex

    =

    "-1"

    role=

    "dialog"

    aria-labelledby=

    "myModalLabel"

    aria-hidden=

    "true"

    >
  41. <div

    class

    =

    "modal-dialog"

    >
  42. <div

    class

    =

    "modal-content"

    >
  43. <div

    class

    =

    "modal-header"

    >
  44. <button

    type

    =

    "button"

    class

    =

    "close"

    data-dismiss=

    "modal"

    aria-hidden=

    "true"

    >

    &times;

    </

    button

    >
  45. <center

    ><h4

    class

    =

    "modal-title"

    id

    =

    "myModalLabel"

    >

    Edit Member</

    h4

    ></

    center

    >
  46. </

    div

    >
  47. <div

    class

    =

    "modal-body"

    >
  48. <div

    class

    =

    "container-fluid"

    >
  49. <form

    id

    =

    "editForm"

    >
  50. <div

    class

    =

    "row"

    >
  51. <div

    class

    =

    "col-md-2"

    >
  52. <label

    class

    =

    "control-label"

    style

    =

    "position:relative; top:7px;"

    >

    Firstname:</

    label

    >
  53. </

    div

    >
  54. <div

    class

    =

    "col-md-10"

    >
  55. <input

    type

    =

    "text"

    class

    =

    "form-control"

    name

    =

    "efirstname"

    id

    =

    "efirstname"

    >
  56. </

    div

    >
  57. </

    div

    >
  58. <div

    style

    =

    "height:10px;"

    ></

    div

    >
  59. <div

    class

    =

    "row"

    >
  60. <div

    class

    =

    "col-md-2"

    >
  61. <label

    class

    =

    "control-label"

    style

    =

    "position:relative; top:7px;"

    >

    Lastname:</

    label

    >
  62. </

    div

    >
  63. <div

    class

    =

    "col-md-10"

    >
  64. <input

    type

    =

    "text"

    class

    =

    "form-control"

    name

    =

    "elastname"

    id

    =

    "elastname"

    >
  65. </

    div

    >
  66. </

    div

    >
  67. </

    div

    >
  68. </

    div

    >
  69. <div

    class

    =

    "modal-footer"

    >
  70. <button

    type

    =

    "button"

    class

    =

    "btn btn-default"

    data-dismiss=

    "modal"

    ><span

    class

    =

    "glyphicon glyphicon-remove"

    ></

    span

    >

    Cancel</

    button

    >
  71. <button

    type

    =

    "button"

    id

    =

    "editbutton"

    class

    =

    "btn btn-warning"

    ><span

    class

    =

    "glyphicon glyphicon-check"

    ></

    span

    >

    Update</

    a

    >
  72. </

    form

    >
  73. </

    div

    >

  74. </

    div

    >
  75. </

    div

    >
  76. </

    div

    >

  77. <!-- Delete -->
  78. <div

    class

    =

    "modal fade"

    id

    =

    "delmem"

    tabindex

    =

    "-1"

    role=

    "dialog"

    aria-labelledby=

    "myModalLabel"

    aria-hidden=

    "true"

    >
  79. <div

    class

    =

    "modal-dialog"

    >
  80. <div

    class

    =

    "modal-content"

    >
  81. <div

    class

    =

    "modal-header"

    >
  82. <button

    type

    =

    "button"

    class

    =

    "close"

    data-dismiss=

    "modal"

    aria-hidden=

    "true"

    >

    &times;

    </

    button

    >
  83. <center

    ><h4

    class

    =

    "modal-title"

    id

    =

    "myModalLabel"

    >

    Delete Member</

    h4

    ></

    center

    >
  84. </

    div

    >
  85. <div

    class

    =

    "modal-body"

    >
  86. <div

    class

    =

    "container-fluid"

    >
  87. <h5

    ><center

    >

    Firstname: <strong

    ><span

    id

    =

    "dfirstname"

    ></

    span

    ></

    strong

    ></

    center

    ></

    h5

    >
  88. </

    div

    >
  89. </

    div

    >
  90. <div

    class

    =

    "modal-footer"

    >
  91. <button

    type

    =

    "button"

    class

    =

    "btn btn-default"

    data-dismiss=

    "modal"

    ><span

    class

    =

    "glyphicon glyphicon-remove"

    ></

    span

    >

    Cancel</

    button

    >
  92. <button

    type

    =

    "button"

    id

    =

    "delbutton"

    class

    =

    "btn btn-danger"

    ><span

    class

    =

    "glyphicon glyphicon-trash"

    ></

    span

    >

    Delete</

    button

    >
  93. </

    div

    >

  94. </

    div

    >
  95. </

    div

    >
  96. </

    div

    >

add.php

This is our code in adding data to our database.

  1. <?php
  2. include

    (

    'conn.php'

    )

    ;
  3. if

    (

    isset

    (

    $_POST

    [

    'firstname'

    ]

    )

    )

    {
  4. $firstname

    =

    $_POST

    [

    'firstname'

    ]

    ;
  5. $lastname

    =

    $_POST

    [

    'lastname'

    ]

    ;

  6. $conn

    ->

    query

    (

    "insert into member (firstname, lastname) values ('$firstname

    ', '$lastname

    ')"

    )

    ;
  7. }

  8. ?>

edit.php

This is our code in updating existing row in our database.

  1. <?php
  2. include

    (

    'conn.php'

    )

    ;
  3. if

    (

    isset

    (

    $_POST

    [

    'efirstname'

    ]

    )

    )

    {
  4. $firstname

    =

    $_POST

    [

    'efirstname'

    ]

    ;
  5. $lastname

    =

    $_POST

    [

    'elastname'

    ]

    ;
  6. $memid

    =

    $_POST

    [

    'memid'

    ]

    ;

  7. $conn

    ->

    query

    (

    "update member set firstname='$firstname

    ', lastname='$lastname

    ' where memberid='$memid

    '"

    )

    ;
  8. }
  9. ?>

delete.php

Lastly, our code in deleting rows.

  1. <?php
  2. include

    (

    'conn.php'

    )

    ;
  3. if

    (

    isset

    (

    $_POST

    [

    'memid'

    ]

    )

    )

    {
  4. $memid

    =

    $_POST

    [

    'memid'

    ]

    ;

  5. $conn

    ->

    query

    (

    "delete from member where memberid='$memid

    '"

    )

    ;
  6. }
  7. ?>

That ends this tutorial. If you have any comment, suggestion or question, feel free to write it below or message me. Happy Coding :)

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.

2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.


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

442,401

317,942

317,951

Top