All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

jQuery Datatables Ajax JSON Example PHP

Last Updated : Mar 11, 2024

jQuery Datatables Ajax JSON Example PHP

In this article we will show you the solution of jQuery datatables ajax json example PHP, here we are going to show you retrieved database data into DataTable structure result with the help of ajax json.

In local server you have to read specified php file then retrieve them by ajax property. Retrieved data converting into json format by json_encode() method and appending on html table to print with table structures.

Step By Step Guide On jQuery Datatables Ajax JSON Example PHP :-

Here we defined table with id, class, width and cellspacing with respective values to apply responsive and css styles.

In script block we defined ready() method where appending DataTable() method with table by specifying table id ‘cusTbl’.

Within this we needs to specify processing, ajax, columns property with respective values.

In external php files we retrieving wanted database table data then converting database data into json and displayed result.

<!DOCTYPE html>
<title>Datatables And Php</title>
<link rel="stylesheet" type="text/css" href="">
<script src=""></script>
<script src="" charset="utf8" type="text/javascript"></script>
    $(document).ready(function() {
            "processing": true,
            "ajax": "fetch_data.php",
            "columns": [
                {data: 'UserName'},
                {data: 'Password'},
                {data: 'Email'}
<table id="cusTbl" class="display" width="100%" cellspacing="0">
<th>User Name</th>
$con = mysqli_connect('localhost', 'root', '', 'dbase') or die("Error " . mysqli_error($con));
$sql = "select UserName, Password, Email from loginrec";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_assoc($result)) {
    $array[] = $row;
$dataset = array(
"echo" => 1,
"totalrecords" => count($array),
"totaldisplayrecords" => count($array),
"data" => $array
echo json_encode($dataset);
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Here we imported open source jquery library support file which is needed for coding using jquery in program.
  5. To get successful result make sure whether you imported open source DataTables css and js cdn links in head block.
  6. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  7. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  8. In script we defined ready() method used to initialize concept which is implement within that there we specified dataTable() with table id ‘cusTbl’.
  9. Within this we need to specify ‘processing-true,ajax property to ‘fetch_data.php (external php file name)’, columns property help to appends retrieved database table columns on html table.
  10. In html block we defined table with id,class,width,cellspacing attributes respective values ‘cusTbl,display,100%,0’. Then we specified five values of header and footer for dataTable data.
  11. In external fetch_data.php file you have to excutes database connection then using select query retrieving all column records from table ‘loginrec’.
  12. Retrieved data storing on array by using while loop iteration then converting into json format with the help of json_encode() methods.
  13. Finally printing on html table to get dataTable structure with database table data’s.
  14. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to use dataTable with ajax,json,php in jquery.

Before execution of program we needs to confirm whether server starts and internet connection because then only that jquery file will supports defined jquery codes in document without any error.

When we executes this program on browser we can see retrieved database table data with dataTable structure.

If you wants to change table data’s value to then you have to provide existing another database table data into html table modify it accordingly.

Make sure whether you changed correctly without missing because that result will give you error instead of retrieved data.

I hope this article on jQuery datatables ajax json example PHP helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪