Inline CRUD in PHP with jQuery AJAX

If you are looking for Inline CRUD in PHP with jQuery AJAX then you are at the right place. In this simple tutorial, we are going to perform Create, Read, Update and Delete functions within the table. In the previous tutorial, we already have seen an example for Inline Adding Data, Inline Editing Data and Inline Deleting Data. Inline CRUD you can add a new row and update the data automatically using jQuery AJAX. You can also check the live demo of this simple tutorial, so you can get an idea and you can try this out, let's start coding. Result Create Markup Editable This simple source code displaying the data from the database and the user can edit the data in the table row and it will save automatically in the database via jQuery AJAX. The source code is,
  1. <table class="tbl-qa">
  2.   <thead>
  3.         <tr>
  4.           <th class="table-header">Product Name</th>
  5.           <th class="table-header">Product Quantity</th>
  6.           <th class="table-header">Action</th>
  7.         </tr>
  8.   </thead>
  9.   <tbody id="table-body">
  10.         <?php
  11.         if(!empty($row_product)) {
  12.         foreach($row_product as $k=>$v) {
  13.           ?>
  14.           <tr class="table-row" id="table-row-<?php echo $row_product[$k]["id"]; ?>">
  15.                 <td contenteditable="true" onBlur="saveToDatabase(this,'product_name','<?php echo $row_product[$k]["id"]; ?>')" onClick="editRow(this);">
  16.                         <?php echo $row_product[$k]["product_name"]; ?>
  17.                 </td>
  18.                 <td contenteditable="true" onBlur="saveToDatabase(this,'product_qty','<?php echo $row_product[$k]["id"]; ?>')" onClick="editRow(this);">
  19.                         <?php echo $row_product[$k]["product_qty"]; ?>
  20.                 </td>
  21.                 <td>
  22.                         <a class="ajax-action-links" onclick="deleteRecord(<?php echo $row_product[$k]["id"]; ?>);">
  23.                                 Delete
  24.                         </a>
  25.                 </td>
  26.           </tr>
  27.           <?php
  28.         }
  29.         }
  30.         ?>
  31.   </tbody>
  32. <div class="add-data-style" id="add-more" onClick="createNew();">Add More</div>
Create Inlide Add This simple script creates a simple row with SAVE and CANCEL options button. After the user entering the data in the row, the user clicks the SAVE button to insert in the database. Then, the data inserted in the database we are going to display it to the table as the new record or new data inserted.
  1. <script>
  2. function addToDatabase() {
  3.   var product_name = $("#product_name").val();
  4.   var product_qty = $("#product_qty").val();
  5.  
  6.           $("#confirmAdd").html('<img src="loaderIcon.gif" />');
  7.           $.ajax({
  8.                 url: "add.php",
  9.                 type: "POST",
  10.                 data:'product_name='+product_name+'&product_qty='+product_qty,
  11.                 success: function(data){
  12.                   $("#new_row_ajax").remove();
  13.                   $("#add-more").show();                 
  14.                   $("#table-body").append(data);
  15.                 }
  16.           });
  17. }
  18. </script>
Create Inline Edit and Delete This script contains the Edit and Delete operations to the row. The script is,
  1. <script>
  2. function save_tO-Database(editableObj,column,id) {
  3.   $(editableObj).css("background","#FFF url(loaderIcon.gif) no-repeat right");
  4.   $.ajax({
  5.     url: "edit.php",
  6.     type: "POST",
  7.     data:'column='+column+'&editval='+$(editableObj).text()+'&id='+id,
  8.     success: function(data){
  9.       $(editableObj).css("background","#FDFDFD");
  10.     }
  11.   });
  12. }
  13.  
  14. function delete_Record_in_Database(id) {
  15.         if(confirm("Are you sure you want to delete this row?")) {
  16.                 $.ajax({
  17.                         url: "delete.php",
  18.                         type: "POST",
  19.                         data:'id='+id,
  20.                         success: function(data){
  21.                           $("#table-row-"+id).remove();
  22.                         }
  23.                 });
  24.         }
  25. }
  26. </script>

Output

  • How to Add
  • Inline Add
  • How to Edit
  • Inline Edit
  • How to Delete
  • Inline Delete
For the full source code, kindly click the "Download Code" button below.

Learn by Examples

Examples are better than thousands of words. Examples are often easier to understand than text explanations. Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.

Add new comment