Jquery数据表添加搜索功能时出错

时间:2018-03-08 14:48:02

标签: javascript php jquery datatables

我的jQuery DataTable出了问题,当我的搜索功能只有四列时,我的数据表正常工作。我的桌子上有7列但是当我尝试在搜索功能中添加另一列时,我收到了这样的信息:

  

DataTables警告:table id = example - 无效的JSON响应。更多   有关此错误的信息,请参阅http://datatables.net/tn/1

这是我的表格的代码

<div class="container" id=subwrapper>
<h1>Employee's Data Table</h1>
    <table id="example" class="display" cellspacing="0" width="100%">
        <thead>
        <tr>
            <th>Employee ID</th>
            <th>First name</th>
            <th>Last name</th>
            <th>Birthday</th>
            <th>Age</th>
            <th>Job Position</th>
            <th>Contact number</th>

        </tr>
        </thead>
        <tfoot>
        <tr>
            <th>Employee ID</th>
            <th>First name</th>
            <th>Last name</th>
            <th>Birthday</th>
            <th>Age</th>
            <th>Job Position</th>
            <th>Contact number</th>

        </tr>
        </tfoot>
    </table>

<script>
    $(document).ready(function(){
        var dataTable=$('#example').DataTable({       
            "processing": true,
            "serverSide":true,
            "ajax":{
                url:"fetchemployee.php",
                type:"post"
            }
        });
    });  
</script>

我的PHP代码:

<?php
 $con=mysqli_connect('localhost','root','','projectmanagementdb')
 or die("connection failed".mysqli_errno());
 $request=$_REQUEST;
$col =array(
  0   =>  'employeeID',
  1   =>  'employeeFirstname',
  2   =>  'employeeLastname',
  3   =>  'employeeBirthday',
  4   =>  'employeeAge',
  5   =>  'employeePosition',
  6   =>  'employeeContactnumber'
);  //create column like table database
$sql ="SELECT * FROM employeeinfo";
$query=mysqli_query($con,$sql);

$totalData=mysqli_num_rows($query);
$totalFilter=$totalData;

//Search function of Jquery Data table
$sql ="SELECT * FROM employeeinfo WHERE 1=1";
  if(!empty($request['search']['value'])){
  $sql.=" AND (employeeID Like '".$request['search']['value']."%' ";
  $sql.=" OR employeeFirstname Like '".$request['search']['value']."%' ";
  $sql.=" OR employeeLastname Like '".$request['search']['value']."%' ";
  $sql.=" OR employeeBirthday Like '".$request['search']['value']."%' )";
  $sql.=" OR employeeAge Like '".$request['search']['value']."%' )";
  $sql.=" OR employeePosition Like '".$request['search']['value']."%' )";
  $sql.=" OR employeeContactnumber Like '".$request['search']['value']."%' 
)";
}
$query=mysqli_query($con,$sql);
$totalData=mysqli_num_rows($query);



$data=array();
while($row=mysqli_fetch_array($query)){
$subdata=array();
$subdata[]=$row[0]; //employeeID
$subdata[]=$row[1]; //employeeFirstname
$subdata[]=$row[2]; //employeeLastname
$subdata[]=$row[3]; //employeeBirthday 
$subdata[]=$row[4]; //employeeAge 
$subdata[]=$row[5]; //employeePosition 
$subdata[]=$row[6]; //employeeContactnumber  
$data[]=$subdata;
}
$json_data=array(
"draw"              =>  intval($request['draw']),
"recordsTotal"      =>  intval($totalData),
"recordsFiltered"   =>  intval($totalFilter),
"data"              =>  $data
);

echo json_encode($json_data);
?>

0 个答案:

没有答案