DataTables warning: table id=user_data - Invalid JSON response. For more information about this erro

DataTables warning: table id=user_data - Invalid JSON response. For more information about this erro

JDasJDas Posts: 1Questions: 1Answers: 0

index.php

<!doctype html>
<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css"
        href="https://cdn.datatables.net/v/bs5/jq-3.6.0/dt-1.11.3/datatables.min.css" />

    <title>Jquery DataTable CRUD</title>
</head>

<body>
    <div class="container-fluid">
        <div class="w-100 d-flex align-item-center justify-content-center">
            <div class="col-md-8 p-2">
                <table id="user_data" class="table table-striped">
                    <thead>
                        <tr>
                            <th scope="col">sl No.</th>
                            <th scope="col">Name</th>
                            <th scope="col">Phone</th>
                            <th scope="col">Email</th>
                            <th scope="col">Address</th>
                            <th scope="col">Action</th>
                        </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
            </div>
        </div>
    </div>



    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script type="text/javascript" src="https://cdn.datatables.net/v/bs5/jq-3.6.0/dt-1.11.3/datatables.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        $(document).ready(function () {
            // var dataTable = $('#datatable').DataTable({
            //     'processing': true,
            //     'serverSide': true,
            //     'order': [],
            //     'ajax': {
            //         'type' : 'POST',
            //         'url' : 'backend.php',
            //     },
            //     'columnDefs': [{
            //         'target': [0, 5],
            //         'orderable': false,
            //     }, ],
            // });
            $.ajax({
                type: "POST",
                url: "backend.php",
                // data: "data",
                dataType: "JSON",
                success: function (data) {
                    $('#user_data').DataTable({
                        processing: true,
                        serverSide: true,
                        deferRender: true,
                        ordering: false,
                        pagingType: 'full_numbers',
                        'columnDefs': [{
                            'target': [0, 5],
                            'orderable': false,
                        }, ],
                    });
                }
            });
        });
    </script>

</body>

</html>

backend.php'

```
<?php
$servername = "localhost";
$username = "root";
$password = "";
$db = "jquerydatatable";

// Connection
$conn = mysqli_connect($servername,$username, $password, $db);

// Check if connection is
// Successful or not
if (!$conn) {
die("Connection failed: "
. mysqli_connect_error());
}
// echo "all set";
// if(isset($_POST['data']))
// {
$query = "SELECT * FROM mytable";
$res = mysqli_query($conn, $query);
$count_row = mysqli_num_rows($res);

// for search box
if(isset($_POST['search']['value'])){

    $serachValue = $_POST['search']['value'];

    $query .= "WHERE name LIKE '%".$serachValue."%'";
    $query .= "WHERE phone LIKE '%".$serachValue."%'";
    $query .= "WHERE email LIKE '%".$serachValue."%'";
    $query .= "WHERE address LIKE '%".$serachValue."%'";
}

// for order
if(isset($_POST['order'])){
    $column = $_POST['order'][0]['column'];
    $order = $_POST['order'][0]['dir'];

    $query .= "ORDER BY '".$column."' ".$order;
}
else{
    $query .= "ORDER BY id ASC";
}

// pegination
// if(isset($_POST["length"]) != -1){
//     $query .= "LIMIT ".$_POST['start'].", ".$_POST['length'];
// }

$data = array();

$res2 = mysqli_query($conn,$query);
$filterd_rows = mysqli_num_rows($res2);
while($arr = mysqli_fetch_assoc($res2)){
    $subArray = array();
    $subArray[] = $arr['id'];
    $subArray[] = $arr['name'];
    $subArray[] = $arr['phone'];
    $subArray[] = $arr['email'];
    $subArray[] = $arr['address'];
    $subArray[] = '<div class="btn-group" role="group" aria-label="Action Buttons">
    <button type="button" class="btn btn-sm btn-success">Edit</button>
    <button type="button" class="btn btn-sm btn-danger">Delete</button>
    </div>';

    $data[] = $subArray;
}

$output = array(
    "draw" => intval(isset($_POST['draw'])),
    'recordsTotal' => $count_row,
    'recordsFiltered' => $filterd_rows,
    'data' => $data,
);


// echo json_encode($output);
echo json_encode($output);
// echo json_encode(array("data" => $output));

// }

<?php > ``` ?>

HERE SHOW LENGTH , START AND DRAW IS UNDEFIND THST IS WHAY I WAS COMMENT IT ... PLEASE CHECK MY CODE AND PLEASE GIVE ME THE REPLY

Sign In or Register to comment.