HTML5 Inline Editing with PHP, MYSQL & jQuery Ajax

This tutorial help to understand HTML5 inline editing with PHP and MYSQL,in Previous tutorial HTML inline editing,We have learn how to create any HTML control inline editable using contenteditable attribute.Inline Editable will work on only HTML5 supported browser.

There are a lot of jQuery plugin which also provide inline editable functionality but using HTML5 you can do very easily without any overhead of jquery library.You just need some ajax jquery coding to update data into MySQL database to update record.This will help on single column updated instead of whole form data.

html5 inline editing with php demo

How will Work inline Editing with HTML5,PHP and Mysql

We will create listing all records into table.We will add contenteditable attribute on all 'td's (

) of table where i need provide inline editable functionality.

Whenever user will change TD data, i will capture event 'focusout' using jQuery and get updated data of td, finally call php function to updated data into mysql using changed td data.

Following files will participate in this example

  1. connection.php : This file used to create connection with mysql
  2. index.php : This file used to display records into table and inline editing option
  3. server.php : This file will call on ajax request and update data into mysql

Also checkout other related tutorials,

Step 1: include bootstrap css file and jQuery library into head section of page.

Step 2: Created Connection file connection.php for database connection with php.

Step 3: Get records from mysql and store in variable,put below code into index.php file.

Here i am featching data from mysql for display data into HTML table.You need to replace table name 'employee' with your table name.

Step 3: Create html table and iterate records into row.

Here i am using step-2 $queryRecords variable and iterate on tr using php foreach() function to display table data,You can see i have added hard coded col-index='*' attribute on each table td for identification of column name.I have also addaed oldVal='*' attribute to store old valve of table, if the new and old value is same we will not send server request.
i am also adding data-row-id="" attribute on on each tr of table to indentify row-id of data.This 'id' will be primary key of employee table.This tells for which row of column we are updating.

Step 4: Created AjAX jquery request to update data into mysql using server side php script.

Here You can see i am attaching event 'focusout' on each td of table and send request if the value of td changed, first I am getting changed value td and then get row-id of data tuple with column-id.Aggregate all params and send to server for update column value.

Step 5: Now update data into mysql using server.php.

This is a simple code php code , i haven’t added sql injection prevent code and not combined all validate conditions into complex php code.

Conclusion

I have explain how to implement HTML5 inline editing with PHP,MYSQL and jQuery AJAX.You can easily integrate inline editing with in your application using HTML5 contenteditable attribute.

Download Code and Demo Link

  • Gøulart Júniør

    Can you help me? Only the first column is editable, when I try any other column the change reflect in the first one.

    • The reason was ‘$sql = “UPDATE employee SET “.$columns[$colIndex].” = ‘”.$colVal.”‘ WHERE id='”.$rowId.”‘”;’, i am not sending updated colindex which want to update.

  • Gøulart Júniør

    Only first column is changed, is that the reason?
    $colIndex = $rowId = 0;

  • Zhiend-

    hy, thanks the script before but in there i have a question :
    how to make other coumn can be editable. and when iam try to edit the other column the reflect is in first column.

    • i verified in demo,its working fine,can u please share plunker so that i can find your issue.

      • Teemu Heino

        No it’s not working, the error handling logic is broken. While $colVal gets the value from $_POST[‘val’] correctly, it changes $error to false. And for that reason $colIndex never get’s the value of $_POST[‘index’] (so it puts the new updated values to the first column whose id is 0), and the $error is changed back to true, which makes the updated row correct again.

  • Hi,
    I sent updated source code in ur mail and updated source code as well,

    • Lothar Velling

      … would have taken a while to find this. Thank you – greetings 😉

  • Lynn

    Hi thank you for this post. it’s really helped.
    And I wondering if I can update the (inline-input-data) with a button to each row, if possible?
    Could you please help me where can I define to follow the button click event?

    • You can handle this using jquery,dynamically add attribute in col and send data to server for save

  • Ankush Kala

    can you please send me the updated code ..

    • i updated zip file on download link, u can download from there,please make sure db connection params are correct.

  • Vinayak Madhav Pande

    Hi, Thank you so much for the post! It’s very helpful.

    I am facing a problem. The data is not loading into the table. I have created an “emplyoee” table in my database with the exact fields and entered the connection details. It would be grate of you could help me out.

    Thanks in advance!

  • Zhuo Yang

    Thank you very much for the post. I learnt from this tutorial but found a problem. The codes work well on my Windows system, but the table does not render with any contents when the codes are implemented in Linux server (Operating System: CentOS 7 64Bit). Does it need any module to support the functionality? Thanks!
    https://uploads.disquscdn.com/images/8c2522f29665bed72e9ba19246a97903ec3a9b26c0dadc1aee3cd8b36146b632.png

    • Have u check col name,linux is case senstive

  • make sure dbname, username and password updated in connnection.php file, the table name should be employee and connection file included into server.php file.

  • ur browser version supported HTML5? I sent u my working source code in ur mailid as well and I hvnt tested on linux.

    • Zhuo Yang

      Thanks for sending me the files. Yes. My browser supports HTML5. And I have also tested your files by cloning the ’employee’ table to my database. There turns nothing for the table on the webpage if I post the files to the Linux server. However, it works perfect on my local computer. Do you think if there is any environment issues?
      Thanks!

  • David

    I have all the code working just fine … however If I enter some text in a Textarea, including some line breaks …. it does not maintain the line breaks and puts all the text on the same line …

    So the line breaks are not making there way to the database and when the page is updated it is one long line of text all mashed together.

    How can I maintain the line breaks when written to the database ?

    • have u check ur text has new line char before the insert query,If yes u can convert special chart in unicode and store db and revert back

  • Jed Sta Ana

    great tutorial sir very useful i tried to mix it up with other codes it was perfect

  • chogan

    Thanks for this post – very clear and helpful in explaining this. I have set it up though I keep getting the error – Failed! updation in mysql when i try to test making a change. Can you give an idea what the problem might be?

    • i think mysql table col would be mis match

  • print sql query and run into mysql query window, if its working fine then should be show in table.

  • Very nice work. A little bug in the code error handling above, but in the download link it is fixed, a simple but hard-to-see fix 🙂 Thank you.

  • I do have one question. It seems that if we are using this on a big table with many rows, it does not work until the page is fully loaded, so if a user loads the table and starts trying to edit right away, those edits are not saved. Do you know of a good way around this? Perhaps preventing the contenteditable from working until the page is fully loaded?

  • Hv u checked req params in netwprk tab under developer/firebug tool

  • Hv u checked req params in network tab under developer/firebug tool.