Live Table Edit And Delete With Pagination Using Jquery Ajax

Live Table Edit And Delete With Pagination Using Jquery Ajax

Although there are many jquery plugins available on the Internet to live edit the data tables but in this article we are writing our own simple php and javascript files to achieve the same target using ajax. I’m a big fan of Shrinivas Tamada (Owner of and highly inspired by him. I always love his posts and tutorials. In this article I’m just extending one of this most popular post (Live table data edit and delete) by implementing the multiselect delete and ajax pagination. The post is very useful for the beginners who want to learn ajax and jquery with php. I’ve used PHP’s OOPS concept and “On” event handler of jquery to reduce the direct function calling in the project. So without wasting your too much time, I move to further and start to write the application.

Dynamically Add Remove Text Box Using Jquery

Live Table Edit And Delete With Pagination Using Jquery Ajax

You will get the following files in the downloaded zip archive:
1. Index.php (Base php file to boot up the application)
2. Database.php (Handles database connection and other mysql methods)
3. Style.css (Mark up for the application to give a beautiful look)
4. Script.js (Create ajax functionality in the project using dom)
5. livetable.sql (Database sql dump to create the users table)

Now First of all we design our database and create the users table

CREATE DATABASE `livetable`;
CREATE TABLE `livetable`.`users`(
`name` VARCHAR(50),
`email` VARCHAR(100),
`country` VARCHAR(50),
`mobile` VARCHAR(50),
PRIMARY KEY (`id`) );

Let’s move next to create the basic markup and style for our application as we always do. I tried to reduce the css code too much but a branded shirt is always looks bad without its buttons. Hope you understood.


/* CSS Document */
body {

#outer_container {
margin:0 auto;
padding: 50px 10px 10px 10px;
border:1px solid #ccc;
font-family:Arial, Helvetica, sans-serif;

input {
border: 1px solid #B4D349;
border-radius: 2px 2px 2px 2px;
padding: 5px;
width: 90%;

position: absolute;
text-align: center;
z-index: 9999;
top: 50%;
left: 50%;

.delall {
background: #1F9DBF;
border: medium none;
color: #FFFFFF;
cursor: pointer;
height: 35px;
margin: 0 0 15px 45px;
padding: 0 5px 2px;

#data_tbl {
font-family:Arial, Helvetica, sans-serif;
border-left:1px solid #ccc;
border-top:1px solid #ccc;

#data_tbl tbody tr:hover{

#data_tbl td,#data_tbl th{
border-right:1px solid #ccc;
border-bottom:1px solid #ccc;

#data_tbl thead {
background: #00b7ea;
background: -moz-linear-gradient(top,  #00b7ea 22%, #009ec3 73%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(22%,#00b7ea), color-stop(73%,#009ec3));
background: -webkit-linear-gradient(top,  #00b7ea 22%,#009ec3 73%);
background: -o-linear-gradient(top,  #00b7ea 22%,#009ec3 73%);
background: -ms-linear-gradient(top,  #00b7ea 22%,#009ec3 73%);
background: linear-gradient(to bottom,  #00b7ea 22%,#009ec3 73%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#00b7ea', endColorstr='#009ec3',GradientType=0 );
color: #FFFFFF;
font-weight: bold;
height: 35px;


#paginator {
margin: 15px auto;
width: 91%;

.pagin {
padding: 10px 0;
font:bold 11px/30px arial, serif;

.pagin * {
padding: 2px 6px;
margin: 2px;

.pagin a {
border:solid 1px #036E92;
padding:6px 7px 5px;

.pagin span, a:hover, .pagin a:active, .pagin span.current {
background: -moz-linear-gradient(center top , #088899 1px, #036E92 1px, #049DB8) repeat scroll 0 0 rgba(0, 0, 0, 0);
color: #FFFFFF;

.pagin span,.current{
padding:8px 7px 7px;

Script.js For Jquery Operations

//JavaScript Document
     load('action=ajax&page=1'); //Load by default first page on load
$(document).on('click','.pagin a', function(){
    load('action=ajax&page='+$(this).attr('id')); //Load data when click on pagination
function load(str) {
   $('#data').load('index.php?'+str,function() {

//Select All with checkbox functionality
$(document).on('click','input:checkbox',function(e) {
  if($(this).hasClass('selall')) {
   } else {
   if($('.selrow:checked').length == $('.selrow').length) {
   } else {

//Functionality for single or multiple delete
  var id;
  if($(this).hasClass('delall')) {
     id = $('.selrow:checked').map(function(){
              return $(this).val();
     } else {
        id = $(this).parents('tr').find('input:hidden').val();
   if($('.selrow:checked').length == 0 && $(this).hasClass('delall')) {
     alert('Please select atleast one row');
   } else if(confirm('Do you really want to delete')) {

//Save updated data in database
function savedata() {
   if($('.modified').length > 0) {
       $tr = $(this);
       $.post('index.php?'+$tr.find(':input').serialize()+'&action=update',function() {});

//Save data when click anywhere on page body
   if(! $(':input')) {

//Show input boxes in row when click on update icon
    $tr = $(this).parents('tr');
    $(this).after('<input type="text" name="'+$(this).attr('class')+'" value="'+$(this).text()+'" maxlength="35" />');

Database.php For Database and Pagination

* Database Class
* @author Manish Jangir
class Database {
const DB_HOST = 'localhost';
const DB_USER = 'root';
const DB_PASSWORD = '';
const DB_NAME = 'livetable';
private $_dbconnect = NULL;
private $_table = 'users';
private $_adj = 4;
private $_tpages = 0;
private $_limit = 5;
private $_offset= 0;
private $_page = 1;
private $_prev_lbl = '&lsaquo; Prev';
private $_next_lbl = 'Next &rsaquo;';

public function __construct() {
$this->_dbconnect = mysql_connect(self::DB_HOST,self::DB_USER,self::DB_PASSWORD);
if ($this->_dbconnect) {
$db =  mysql_select_db(self::DB_NAME,$this->_dbconnect);
} else {
$this->_page = (isset($_REQUEST['page']) && !empty($_REQUEST['page']))?$_REQUEST['page']:1;
$this->_offset = ($this->_page - 1) * $this->_limit;

private function total() {
$result = mysql_query("select count(id) AS total FROM $this->_table");
$row = mysql_fetch_array($result);
return $row['total'];

public function get_users() {
$query = mysql_query("SELECT * FROM $this->_table ORDER BY id DESC LIMIT $this->_offset,$this->_limit");
$result = array();
$i = 0;
while($res = mysql_fetch_assoc($query)){
$result[$i] = $res;
return $result;

public function delete($id){
$ids = is_array($id) ? implode(',', $id) : $id;
$query = mysql_query("DELETE FROM $this->_table WHERE id IN ($ids)");
return $this->result($query);
public function insert($data) {
$keys = implode(',', array_keys($data));
$values = "'" . implode("','", array_values($data)) . "'";
$query = mysql_query("INSERT INTO $this->_table ($keys) VALUES ($values)");
return $this->result($query);

public function update($data) {
$id = $data['id'];
$query = "UPDATE $this->_table SET ";
foreach ($data as $key => $value) {
$params[] = $key." = '".$value."'";
$query .= implode(',', $params)." WHERE id = $id";
return $this->result(mysql_query($query));

private function result($q) {
return $q ? true : false;

public function paginate() {
$this->_tpages = ceil($this->total()/$this->_limit);
$out = '<div>';
if($this->_page == 1) {
$out .= "<span>$this->_prev_lbl</span>";
} else {
$out .= "<a href='javascript:void(0);' id='".($this->_page-1).")'>$this->_prev_lbl</a>";
$out.= ($this->_page>($this->_adj+1)) ? "<a href='javascript:void(0);' id='1'>1</a>" : '';
$out.= ($this->_page>($this->_adj+2)) ? $out.= "...\n" : '';
$pmin = ($this->_page>$this->_adj) ? ($this->_page-$this->_adj) : 1;
$pmax = ($this->_page<($this->_tpages-$this->_adj)) ? ($this->_page+$this->_adj) : $this->_tpages;
for($i=$pmin; $i<=$pmax; $i++) {
if($i==$this->_page) {
$out.= "<span class='current'>$i</span>";
}else {
$out.= "<a href='javascript:void(0);' id='$i'>$i</a>";
$out. ($this->_page<($this->_tpages-$this->_adj-1)) ? $out.= "...\n" : '';
$out.= ($this->_page<($this->_tpages-$this->_adj))? $out.= "<a href='javascript:void(0);' id='$this->_tpages'>$this->_tpages</a>" : '';
if($this->_page<$this->_tpages) {
$out.= "<a href='javascript:void(0);' id='".($this->_page+1)."'>$this->_next_lbl</a>";
}else {
$out.= "<span>$this->_next_lbl</span>";
$out.= "</div>";
return $out;

Index.php Base File

$action = (isset($_REQUEST['action']) && !empty($_REQUEST['action']))?$_REQUEST['action']: NULL;
if(empty($action)) {
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Jquery Ajax Live Table Edit</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<script type="text/javascript" src="jquery-1.10.1.min.js"></script>
<script type="text/javascript" src="script.js"></script>
<div id="outer_container">
<div id="loader" ><img src="loader.gif"></div>
<div id="data" style="position:relative;">
<?php } ?>

<?php if(empty($action)) {?>
<?php }

if($action == 'ajax' || $action == 'update' || $action == 'delete') {
require_once 'database.php';
$db = new Database;
function getTable() {
$data = '<button>Delete Selected</button>
<form><table width="90%" cellspacing="0" cellpadding="2" align="center" border="0" id="data_tbl">
<th width="5%"><input type="checkbox"/></th>
<th width="25%">Name</th>
<th width="25%">Email</th>
<th width="17%">Country</th>
<th width="18%">Mobile</th>
<th width="10%">Action</th>
$i = 1;
$cls = false;
foreach ($db->get_users() as $value) {
$bg = ($cls = !$cls) ? '#ECEEF4' : '#FFFFFF';
$data .='<tr style="background:'.$bg.'">
<td><input type="checkbox" value="'.$value['id'].'"/>
<input type="hidden" value="'.$value['id'].'" name="id" />
<td><span>'.$value['name'].' </span></td>
<td align="center">
<img src="edit.png" title="Update"/>&nbsp;
<img src="delete.png" title="Delete"/>
$data .='</tbody>
<div id="paginator">'.$db->paginate().'</div>';
return $data;

if($action == 'ajax') {
echo getTable();
} else if($action == 'delete') {
echo getTable();
} else if($action == 'update') {

Simple download the zip file and extract in your root folder to run the application straight forward.

Live Table Edit And Delete With Pagination Using Jquery Ajax

5 Responses to “Live Table Edit And Delete With Pagination Using Jquery Ajax”
  1. tester says:


    Great Work dude, but i have one request from you can you email me a different text filed option , i mean to say you have using a input in loop , but if i want to use drop-down , check-box ..etc then how i can add and edit .. can you let me know ? .

    Thanks in advance

  2. Manish says:

    I used this script, it works great while displaying text,
    i rename index.php to member_update.php & changed some required.
    delete works. but update is not changing the text

  3. Martin says:

    I downloaded the demo.
    I connect a table to the database. In this tutorial works only delete row?
    If I try edit record and reload page changes are not saved to the database…


  4. Jamial Hasan says:

    pagination not working ot appearing multiple times for large amount data. please help.

  5. chirag says: