Hello Friends, In this tutorial we are going to learn how to live table Insert, Update, Delete and Fetch data from mysql database using Ajax Jquery in PHP programming language. For this feature I am using html5 contenteditable attribute. With the help of this contenteditable attribute table column will be editable and user can change live table data on single click. I am using Ajax call function for Insert data, update data, delete data and Select data from database. This all things are done on user end without page refresh. Ajax get request from user on front-end and send request to database using php language and done operation at back end and send back request to user without page refresh. This feature is very helpful. I hope this tutorial will help to all.
This is main page on which we have load data, so on this page first we have define one div tag with attribute "id=live_data", we will load data under this tag by using Ajax Jquery code and it will use attribute id as selector in Jquery code.
Then after make this jquery function, which fetch data from table and converted in table format and then after display under div tag with attribute "id=live_data", under this function, it use Ajax method for fetch data from server and display on web page. This function send request to this select.php page.
This php code write on select.php page, because fetch_data() jquery function send request to this page, on this page it will fetch data from table and then convert that data into HTML Table format and send back to fetch_data() function.
You can see under while loop we have store data in table format, in first <td> tag it will store id, in second and third <td> tag attribute we have define class, in second have write class="first_name" and in third we have write class="last_name", we will use this attribute class in jquery code for live edit or update of table data.
Then after in both tag we have also add data attribute tag with two different name. In this tag we have store id of data, value of this data attribute we will use in jquery code while live updating of data.
In Both tag we have also use one more HTML attribute like contenteditable, by using this attribute we can live change the text or value this <td> tag. So this way we have define different attribute in second and third <td> tag.
In Fourth <td> tag we have define delete button for removing live data without page refresh, Under this delete button we have store unique id in data attribute, value of this data attribute we can fetch in Jquery code while live remove or delete of particular records. In this button tag we have add on class="btn_delete". We will use this class as selector in Jquery code.
In this code in outside of while loop we have also define one table row with four <td> tag, in second and third <td> tag we have define unique id attribute to both tag. We have use this id attribute as selector in JQuery code and by using this id we can get the text of <td> tag in Jquery code. In both <td> tag we have write one more attribute like contenteditable, by using this attribute we can live edit the text of this tag. In last <td> tag we have define one button for Add new live records, in that button we have use id attribute for selector in JQuery code. We will write Jquery code for Live Insert or Add new records on this button.
So In backend our code is ready for fetching data and we have already make jquery function for load data on we page, so we have called this function, so when page load, this function will be called and it will load data on web page in HTML Table format.
This JQuery code is for Live Insert or Add of Data into Mysql Table. We have write Jquery code on Button with id="btn_add" on click event. When we have click on This button then it will Insert data into table. Under this first we have fetch text from <td> tag by selecting attribute id. After fetching value from <td> tag then it will check both variable has some value or not. If both value has some value then it will make Ajax request and send to insert.php page. With Ajax request it will send value of both <td> tag to server. After successfully request send and received it will reload table data by calling fetch_data() function.
This is php code written on insert.php page, This page will received data from Ajax request and on this page it will make Insert Query for Inserting or Adding data into Mysql Table and it will send respond to Ajax request by write echo statement.
After Successfully Live Insert or Add data, now we want to edit data, so in Jquery code we have make this edit_data(id, text, column_name) function with three argument. Value of All argument data has been send to edit.php page.
Above code is written under edit.php page, and this page will received data from Ajax request and then after it will make Update query and execute query and update particular id of data in Mysql Table.
Above Jquery code is write for Live Update or Edit of table column first_name, We have write JQuery code on <td> tag with class selector on blur event, So when we have leave first_name <td> tag then this code will execute. Under this function it will received id from data-id1 attribute and text of first name get from <td> class attribute with text() method. This method will fetch text from <td> tag and store into one variable. Then after we have called edit_data() function which send Ajax request to edit.php page and then after it will update or edit first_name table column data.
Above Jquery code is write for Live Update or Edit of table column last_name, We have write JQuery code on <td> tag with class selector on blur event, So when we have leave last_name <td> tag then this code will execute. Under this function it will received id from data-id2 attribute and text of last name get from <td> class attribute with text() method. This method will fetch text from <td> tag and store into one variable. Then after we have called edit_data() function which send Ajax request to edit.php page and then after it will update or edit last_name table column data.
Above JQuery code is write for Live Delete or Remove Mysql table data. We have write JQuery code on button with class="btn_delete", we have use this class as selector in this Jquery code, so When we have click on delete button then this code will execute. Under this first we have get id from button attribute data-id3. In which we have store unique id. Then after it has send Ajax request to delete.php page. With Ajax request it has been send value of id variable to delete.php page. In Ajax request it will received respond from server and then after it has called fetch_data() functio for refresh table table on web page.
Above PHP Code is written on delete.php page. This page has been received data from Ajax request and then after it will delete query and remove or delete data from Mysql Table and send respond to Ajax method.
So this is my sample code for making system like Live table Insert Update Delete and Fetch of Mysql Table data by using PHP Script with Ajax JQuery method. This is a single page web application. You can perform all operation on single page without going to another page. If you have any query or inputs, just comment your query or inputs in comment box. We will help you.