Inline Editing HTML table with jQuery Ajax and PHP

I was doing a project using jqGrid. Its really a good jQuery Grid Plugin to do hard things easily with few lines of code. I saw a feature of inline editing of records in grid. jqGrid offers many features which really makes your grid functional in an exceptional way. Just few lines of codes and you are done. I thought of doing it in core jQuery and PHP. I started looking if some thing similar is already built or not. Then I got few tutorials but the one published in 9lessons is exactly the same. So I started creating by making it a reference.

inline table editing jQuery

                    DOWNLOAD CODE   LIVE DEMO

Note: Our LIVE DEMO has no database connection but you can download the code and try it in your local machine.

In this post I will explain how this code works and how you can use it in your projects. We have a database with a table named “subjects” with four columns  ID, Name, Comments, Replace. Below is the SQL query to create table:

CREATE TABLE IF NOT EXISTS <code>subjects</code> (
 <code>id</code> int(11) NOT NULL AUTO_INCREMENT,
 <code>name</code> varchar(50) DEFAULT NULL,
 <code>comments</code> varchar(50) DEFAULT NULL,
 <code>replace</code> varchar(50) NOT NULL,
 PRIMARY KEY (<code>id</code>)
&lt;p style=&quot;text-align: justify;&quot;&gt;

Now Lets write our main PHP page which will fetch data from table and display it into an HTML table. Here’s the main code for that:

<table border=1 width="900px" class="mGrid">
<th>Subject</th><th>Comments</th><th>First thought</th></tr>
$query = mysql_query("select * from subjects");

$id= $data['id'];
$comments = $data['comments'];
$replace_with = $data['replace'];

<tr id="<?php echo $id; ?>" class="tredit" />

<?php } else {?>
 <tr id="<?php echo $id ?>"bgcolor="#C2C2C2" class="tredit">
<?php }?>
 <td width="33%" class="edittbl">
 <span id="first_<?php echo $id; ?>" class="text"><?php echo $name; ?> </span>
 <input type="text" class="ip" id="first_ip_<?php echo $id ?>" value="<?php echo $name; ?>" </input></td>
<td width="33%" class="edittbl">
<span id="second_<?php echo $id; ?>" class="text"><?php echo $comments; ?> </span>
<input type="text" class="ip" id="second_ip_<?php echo $id ?>" value="<?php echo $comments; ?>" </input></td>
<td width="33%" class="edittbl">
<span id="third_<?php echo $id; ?>" class="text"><?php echo $replace_with; ?> </span>
<input type="text" class="ip" id="third_ip_<?php echo $id ?>" value="<?php echo $replace_with; ?>" </input></td>
<p style="text-align: justify;">

This code may look a little bit hard-to-understand type (for new devs) but its not. Let me explain this. We start our HTML table with heading section wrapped in <th> </th>tag. Then we fetch data from MySQL using while loop and display it into our table.

You would have noticed “IF” condition in our code. This is just to give styling to alternate rows in our table. Notice that we are dynamically generating ID’s using PHP. Its really important because we will need to catch these ID’s in jQuery to do manipulations. Now lets have a look at our jQuery code:

var ID=$(this).attr('id');

var ID=$(this).attr('id');
var first=$("#first_ip_"+ID).val();
var second=$("#second_ip_"+ID).val();
var third=$("#third_ip_"+ID).val();
var dataString = 'id='+ ID +'&name='+first+'&comment='+second+'&replace='+third;
<p style="text-align: justify;">

This jQuery code simply hides all the elements with ID’s first_1, second_1 and third_1. And displays the input box in front of user so that he can enter new data. We have given input boxes ID’s with prefix first_ip_, second_ip_ and third_ip_. As soon as user enters his data change() function comes into picture and we store  values of all the cells in variable to prepare a datastring. We will send this datastring via ajax call to “post_tabel.php” which will simply update the table in database.

In “post_tabel.php” we extract all the information from recieved datastring and then update the table using update sql query.

Code for post_tabel.php:


$sql = &quot;update subjects set <code>name</code>='$name',<code>replace</code>='$replace',<code>comments</code>='$comment' where <code>id</code>='$id'&quot;;
&lt;p style=&quot;text-align: justify;&quot;&gt;

Note: Code shown in this post is just the glimpse of working code. Please download whole code from here.

Inline editing may come in handy when you want to give users easiest way to update table records. Next time I will share how to insert/delete a complete new row to/from an HTML table on the fly.

About sanjeev

Dreamer, Blogger and Thinker. I love to help people in solving their problems. You can also join me HERE

Related posts:

14 Responses so far.

  1. The table is in the tutorial:
    Subject – Comments – First thought
    I would create different as
    First thought
    I try to break the table as above, but any changes I make to the code working.

  2. want to downl;oad code.. but error is displying even after subscription

  3. I am wondering how I might make the columns sortable in this example. I have it working perfectly with my database info and the inline updating of db info… now just need to see about sorting if I am going to use this on my site. Have you posted anything about making the columns sortable? Or can you do that so that everyone benefits from the info?


  4. Hi,

    I am not able to download the code. It says following although i subscribed yesterday.
    Sorry, We dont have your email. Please Subscribe Below.

    Thank you.

  5. Hi, thank you for this code. Adding a fourth column data and text input in js file and php files. But I can’t edit or delete. When i click input data is lost, and the text does not see. Help me please.

Leave a Reply

Your email address will not be published. Required fields are marked *