AutoFill table in pageload using ajax , json , php and dataTable 1.1o

AutoFill table in pageload using ajax , json , php and dataTable 1.1o

eldhosee2eldhosee2 Posts: 14Questions: 9Answers: 0

i try to fill a department table on pageload.

i have a index.php page contain a table like this

ID Name
ID Name

and i try to fill this table by fetching values from mysql database and convert into json object and pass to index.php page
but i get an error

" jquery.dataTables.min.js:48 Uncaught TypeError: Cannot read property 'length' of undefined " in browser console.
there is any problem in json convertion.
please help me..

index.php

$(document).ready(function() { $('#example').dataTable({ "ajax": { url: "getDepartment.php", type: "POST", dataType: "json" } }); } );

getdepartment.php

<?php
//database connnection

$qry="select dept_id,dept_name from tb_department ";
$result=mysqli_query($connection,$qry);           
$arr=array();
$i=0;
while($data=mysqli_fetch_array($result)){
    $arr[$i][0]=$data['dept_id'];
    $arr[$i][1]=$data['dept_name'];
    $i++;
}

echo json_encode($arr);

<?php > ?>
This discussion has been closed.