0

I'm still learning this but I'm getting an just [Object object] please help. I've tried to follow the documentation in Datatables but still cant get it right. Hope you can help me... thanks

Here's the code

Javascript

 $(document).ready(function() {
        $.ajax({
            url: "includes/view_ajax.php",
            type: "POST",
            cache: false,
            dataType: 'json',
            success: function(dataResult){
                alert( JSON.stringify(dataResult) );
                $('#memberdata').DataTable({
                    "searching": true,
                    "aaData": [dataResult],
                    "aoColumns": [
                      { "sTitle": "PlateNo" },
                      { "sTitle": "Make" },
                      { "sTitle": "Series" }
                    ] 
                });
            }
        });

PHP

<?php

$columns = array( 
// datatable column index  => database column name
    0 => 'PlateNo',
    1 => 'Make',
    2 => 'Series',
);
include 'database.php';
$sql = "SELECT * FROM vehicleinfo";
$res = mysqli_query($conn, $sql) or die("Error: ".mysqli_error($conn));
$dataArray = array();
while( $row = mysqli_fetch_array($res) ) {


                 $PlateNo = $row["PlateNo"];
                 $Make = $row["Make"];
                 $Series = $row["Series"];
                 
                $dataArray [] =  array("PlateNo" => $PlateNo,
                                        "Make" => $Make,
                                        "Series" => $Series);


}

echo json_encode($dataArray);

?>
2
  • Are you wrapping your database results in an array twice? First here: $dataArray [] = ... - and then again here: "aaData": [dataResult],. To double-check, you can edit your question to show us the JSON text output from echo json_encode($dataArray);. Commented Apr 19, 2021 at 16:45
  • Also, I do believe the solution noted by @mohammedazar is necessary (it just may not be sufficient). Commented Apr 19, 2021 at 16:45

1 Answer 1

1
$('#memberdata').DataTable({
                    "searching": true,
                    "aaData": [dataResult],
                    "aoColumns": [
                      { "sTitle": "PlateNo" , mData:'PlateNo' },
                      { "sTitle": "Make", mData:'Make' },
                      { "sTitle": "Series", mData:'Series' }
                    ] 
                });

You Need to add mData Property to object of the Column arrays

Sign up to request clarification or add additional context in comments.

1 Comment

@japs: If something doesn't work, it can be useful to explain what actually happened, and also provide any related error messages. In this case, I do believe the changes are necessary (but they may not be sufficient, on their own, to solve the problem).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.