How to Inline Edit Data in Table using PHP and jQuery

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.  
  7. INSERT INTO `members` (`id`, `firstname`, `lastname`, `address`) VALUES
  8. (1, 'neovic', 'devierte', 'silay city'),
  9. (2, 'gemalyn', 'cepe', 'carmen, bohol');
  10.  
  11.  
  12. ALTER TABLE `members`
  13.   ADD PRIMARY KEY (`id`);
  14.  
  15. ALTER TABLE `members`

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.         <title></title>
  3.         <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
  4.         <script src="jquery.min.js"></script>
  5. </head>
  6. <div class="container">
  7.         <h1 class="page-header text-center">How to Inline Edit using jQuery</h1>
  8.         <div class="row">
  9.                 <div class="col-sm-8 col-sm-offset-2">
  10.                         <div id="response" class="alert text-center" style="display:none;">
  11.                                 <button type="button" class="close" id="clearMsg"><span aria-hidden="true">&times;</span></button>
  12.                                 <span id="message"></span>
  13.                         </div>
  14.                         <table class="table table-bordered table-striped">
  15.                                 <thead>
  16.                                         <tr>
  17.                                                 <th>ID</th>
  18.                                                 <th>Firstname</th>
  19.                                                 <th>Lastname</th>
  20.                                                 <th>Address</th>
  21.                                                 <th>Action</th>
  22.                                         </tr>
  23.                                 </thead>
  24.                                 <tbody id="tbody">
  25.                                 </tbody>
  26.                         </table>
  27.                 </div>
  28.         </div>
  29. </div>
  30. <script src="app.js"></script>
  31. </body>
  32. </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.  
  44.                                         //populate table with updated row
  45.                                         row.find('.editValue.firstname').html(response.member.firstname);
  46.                                         row.find('.editValue.lastname').html(response.member.lastname);
  47.                                         row.find('.editValue.address').html(response.member.address);
  48.  
  49.                                         row.find('.editInput.firstname').val(response.member.firstname);
  50.                                         row.find('.editInput.lastname').val(response.member.lastname);
  51.                                         row.find('.editInput.address').val(response.member.address);
  52.                                 }
  53.                         }
  54.                 });
  55.         });
  56.         //clear msg
  57.         $('#clearMsg').click(function(){
  58.                 $('#response').hide();
  59.         });
  60. });
  61.  
  62. function fetch(){
  63.         $.ajax({
  64.                 method: 'GET',
  65.                 url: 'fetch.php',
  66.                 success: function(response){
  67.                         $('#tbody').html(response);
  68.                 }
  69.         });
  70. }

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.  
  4.         $sql = "SELECT * FROM members";
  5.         $query = $conn->query($sql);
  6.         while($row = $query->fetch_array()){
  7.                 ?>
  8.                 <tr id="<?php echo $row['id']; ?>">
  9.                         <td>
  10.                                 <?php echo $row['id']; ?>
  11.                         </td>
  12.                         <td>
  13.                                 <span class="editValue firstname"><?php echo $row['firstname']; ?></span>
  14.                                 <input type="text" class="form-control editInput firstname" name="firstname" value="<?php echo $row['firstname']; ?>" style="display:none;">
  15.                         </td>
  16.                         <td>
  17.                                 <span class="editValue lastname"><?php echo $row['lastname']; ?></span>
  18.                                 <input type="text" class="form-control editInput lastname" name="lastname" value="<?php echo $row['lastname']; ?>" style="display:none;">      
  19.                         </td>
  20.                         <td>
  21.                                 <span class="editValue address"><?php echo $row['address']; ?></span>
  22.                                 <input type="text" class="form-control editInput address" name="address" value="<?php echo $row['address']; ?>" style="display:none;"> 
  23.                         </td>
  24.                         <td>
  25.                                 <button class="btn btn-primary editbutton"><span class="glyphicon glyphicon-edit"></span> Edit</button>
  26.                                 <button class="btn btn-success savebutton" style="display:none;"><span class="glyphicon glyphicon-floppy-disk"></span> Save</button>
  27.                         </td>
  28.                 </tr>
  29.                 <?php
  30.         }
  31.  
  32.        
  33.                                
  34. ?>

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.  
  4.         $output = array('error' => false);
  5.        
  6.         $id = $_POST['id'];
  7.         $firstname = $_POST['firstname'];
  8.         $lastname = $_POST['lastname'];
  9.         $address = $_POST['address'];
  10.  
  11.         $sql = "UPDATE members SET firstname = '$firstname', lastname = '$lastname', address = '$address' WHERE id = '$id'";
  12.         $query = $conn->query($sql);
  13.  
  14.         if($query){
  15.                 $output['message'] = 'Member updated successfully';
  16.                 //return the updated member
  17.                 $output['member'] = array(
  18.                         'firstname' => $firstname,
  19.                         'lastname' => $lastname,
  20.                         'address' => $address
  21.                 );
  22.         }
  23.         else{
  24.                 $output['error'] = true;
  25.                 $output['message'] = 'Cannot update member';
  26.         }
  27.  
  28.         echo json_encode($output);
  29.  
  30. ?>

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 :)

Add new comment