提问者:小点点

使用PHP和Ajax的服务器端数据表


这是我的数据表代码:

$('#open').DataTable( {
        select: true,
        "processing": true,
        "sAjaxSource": "booked1.php",
        "serverside": true,
        "columns" :[ {
            "data" : "name"
        }, {
            "data" : "date1"
        }, {
            "data" : "bookingtoken"
        }, {
            "data" : "insurance"
        }]
    } );

这是我的ajax调用:

$("#submit").on('click', function () {

 $('#loadarModal').modal({backdrop: 'static', keyboard: false});
    var date = $("#date").val();
    //alert(date);
    if (date == '') {
        $("#dateText").show();
        $("#dateText").html("Please select date");
        $("#loadarModal").modal('hide');
    } else {
        $("#dateText").hide();
        //alert("can processd");
        var data = $("#form").serialize();
        $.ajax({
            type: 'POST',
            url: 'booked1.php',
            data: {
                date: date
            },
            cache: false,
            dataType: "html",
            success: function (response) {
                alert(response);
                if(response==''){

                }
                $("#booking").html(response);
                $("#loadarModal").modal('hide');

            }


        });
    }

    });

这是我的PHP脚本:

 include 'd_b_con.php';

  if(isset($_POST['date'])){

$date=$_POST['date'];

$query=mysqli_query($conn,"select date as date ,tokenno as tokenno ,inusrance as inusrance,bookingtoken as bookingtoken ,
                                  fname as fname,lname as lname , status as status from at_booking where date='$date'");
$data=array();
while($row1=mysqli_fetch_array($query)){
    $data[] = $row1;
    $date1=$row1['date'];
    $tokenno=$row1['tokenno'];
    $bookingtoken=$row1['bookingtoken'];
    $fname=$row1['fname'];
    $lname=$row1['lname'];
    $status=$row1['status'];
    $insurance=$row1['inusrance'];
    $name=$fname.' '.$lname;

    echo '<tr>';
    echo "<td>$name </td>";
    echo "<td> $date1 </td>";
    echo "<td>$bookingtoken </td>";
    echo "<td>$insurance </td>";
    echo '</tr>';

    $result=array(
        "name" => $name,
        "date1" => $date,
        "bookingtoken" => $bookingtoken,
        "insurance" => $insurance
    );
    echo json_encode($result);
}

这是我第一次使用服务器端数据表。我得到的错误,如"数据表警告:表id=open-无效的JSON响应。有关此错误的详细信息,请参阅http://datatables.net/tn/1"有人能指导我如何将数据表服务器端用于我的代码吗?


共2个答案

匿名用户

尝试更改此代码,

$result = [];
$result[]=array(
        "name" => $name,
        "date1" => $date,
        "bookingtoken" => $bookingtoken,
        "insurance" => $insurance
    );
echo json_encode($result);
die;

一旦检查在网络-

匿名用户

你真的把事情复杂化了。您永远不应该呼出实际的

$data = array();
while( $row1 = mysqli_fetch_assoc($query) ){
  $row1['name'] = $row1['fname'].' '.$row1['lname'];
  $row1['date1'] = $row1['date']; //??
  $data[] = $row1;
}
echo json_encode($data);

更新。你可能需要使用

echo json_encode( array('data' => $data) );

如果未将dataSrc设置为'