Duriel
Duriel

Reputation: 3

Jquery Datatable problem. Ajax call to PHP recieving json-data

I try to create an jquery_dataTable. It works quite well with the documentation https://datatables.net/examples/api/row_details.html

Now I try to change the call from "ajax": "objects.txt" in "ajax": "some.php" ändern.

My HTML-Table:

<table id="systeme" class="display" style="width:100%">
    <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Email</th>
        </tr>
    </thead>
    <tbody></tbody>
    <tfoot>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Email</th>
        </tr>
    </tfoot>
</table>

The ajax-call to recieve the data is:

$(document).ready(function() {
    $.ajax({
    type : 'POST',
    url  : 'some.php',
    dataType: 'json',
    //cache: false,
    success :  function(result)
    {
      console.log(result);
      $('#systeme').DataTable({
        "searching": false, 
        "aaData": [result], //get the array data from the ajax call.
        "aoCcolumns": [
           {
             "className":      'details-control',
             "orderable":      false,
             "data":           null,
             "defaultContent": ''
           },
             { "result": "ID" },
             { "result": "Name" },
             { "result": "Email" }
         ],
         "order": [[1, 'asc']]
        });
    }
});

In the PHP file I connect to a database and recieve the information.

$conn = connectDB();
$dataArray = array();
$sql = "SELECT ID, Name, Email FROM `person` WHERE 1";
$result = $conn->query($sql);
if($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $dataArray[] = $row["ID"];
        $dataArray[] = $row["Name"];
        $dataArray[] = $row["Email"];
    }
}
closeDB($conn);

echo json_encode($dataArray);

When I check the loggs I recieve all relevant data. They´re formatted like

0: "1"
1: "Tom"
2: "mail@mail"
3: "2"
4: "Tim"
5: "mail@mail"
6: "3"
7: "Daniel"
8: "mail@mail"

But inside of my table is only one entrie (the first entrie). I don´t know how to format the json file correct or to handly the data right. I tried a lot and for many hours to come to this point, but now I need some help.

I´m pretty new in all of this stuff and an answer would be great

thanks

Timo

Upvotes: 0

Views: 85

Answers (2)

bharat parmar
bharat parmar

Reputation: 302

You need to remove square bracket from result (DataTable's property aaData) and should show like :

 "aaData": result,

And need other changes in your PHP file (as you have not encode JSON string properly via while loop) :

$conn = connectDB();
$sql = "SELECT ID, Name, Email FROM `person` WHERE 1";
$result = $conn->query($sql);
if($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $dataArray = array();
        $dataArray[] = $row["ID"];
        $dataArray[] = $row["Name"];
        $dataArray[] = $row["Email"];
        $dataResult[] = $dataArray; 
    }
}
closeDB($conn);

echo json_encode($dataResult);

Upvotes: 1

lamtacvu
lamtacvu

Reputation: 685

Your data i thinks it should be set like this :

 while($row = $result->fetch_assoc()) {
    $dataArray[] = array($row["ID"], $row["Name"], $row["Email"] );        
}

Upvotes: 0

Related Questions