Test failed with DataTables Editor generator

Test failed with DataTables Editor generator

savanphrsavanphr Posts: 5Questions: 1Answers: 0

Hello,
I am trying to implement a code with datatables generator package with PHP server side and mysql for the database.
I installed the package on my computer in EasyPHP.
When running the code , on the first page (fetch data from mysql) I got this message and nothing is printed in the list :

can anyone could help me ?

DataTables warning: table id=UAT - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1

I checked the json response and can see that It sounds good :

connect database : uat{"data":[{"DT_RowId":"row_1","autor":"Rob","date":"Thu, 1 Feb 18","desc":"Insert 1","status":"On going","comment":"ok"},{"DT_RowId":"row_2","autor":"Steve","date":"Wed, 7 Feb 18","desc":"test insert 2","status":"On Going","comment":"Running ?"},{"DT_RowId":"row_3","autor":"Bob","date":"Thu, 8 Feb 18","desc":"Insert 3","status":"Testing","comment":"marre"},{"DT_RowId":"row_4","autor":"","date":null,"desc":"","status":"","comment":""}],"options":[],"files":[]}

Here is my sql table code :
$db->sql( "CREATE TABLE IF NOT EXISTS UAT (
id int(10) NOT NULL auto_increment,
autor varchar(255),
date date,
desc text,
status varchar(255),
comment text,
PRIMARY KEY( id )
);" );

my javascript code :

/*
* Editor client script for DB table UAT
* Created by http://editor.datatables.net/generator
*/

(function($){

$(document).ready(function() {
var editor = new $.fn.dataTable.Editor( {
ajax: 'php/table.UAT.php',
table: '#UAT',
fields: [
{
"label": "Autor:",
"name": "autor"
},
{
"label": "Date:",
"name": "date",
"type": "datetime",
"format": "ddd, D MMM YY"
},
{
"label": "Desc:",
"name": "desc",
"type": "textarea"
},
{
"label": "Status:",
"name": "status"
},
{
"label": "Comment:",
"name": "comment",
"type": "textarea"
}
]
} );

var table = $('#UAT').DataTable( {
    dom: 'Bfrtip',
    ajax: 'php/table.UAT.php',
    columns: [
        {
            "data": "autor"
        },
        {
            "data": "date"
        },
        {
            "data": "desc"
        },
        {
            "data": "status"
        },
        {
            "data": "comment"
        }
    ],
    select: true,
    lengthChange: false,
    buttons: [
        { extend: 'create', editor: editor },
        { extend: 'edit',   editor: editor },
        { extend: 'remove', editor: editor }
    ]
} );

} );
//alert("uat.js");
}(jQuery));

my php code :
<?php

/*
* Editor server script for DB table UAT
* Created by http://editor.datatables.net/generator
*/

// DataTables PHP library and database connection
include( "lib/DataTables.php" );

// Alias Editor classes so they are easy to use
use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Mjoin,
DataTables\Editor\Options,
DataTables\Editor\Upload,
DataTables\Editor\Validate,
DataTables\Editor\ValidateOptions;

// The following statement can be removed after the first run (i.e. the database
// table has been created). It is a good idea to do this to help improve
// performance.
$db->sql( "CREATE TABLE IF NOT EXISTS UAT (
id int(10) NOT NULL auto_increment,
autor varchar(255),
date date,
desc text,
status varchar(255),
comment text,
PRIMARY KEY( id )
);" );

// Build our Editor instance and process the data coming from _POST
Editor::inst( $db, 'UAT', 'id' )
->fields(
Field::inst( 'autor' ),
Field::inst( 'date' )
->validator( Validate::dateFormat( 'D, j M y' ) )
->getFormatter( Format::dateSqlToFormat( 'D, j M y' ) )
->setFormatter( Format::dateFormatToSql( 'D, j M y' ) ),
Field::inst( 'desc' ),
Field::inst( 'status' ),
Field::inst( 'comment' )
)
->process( $_POST )
->json();

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    What is the server responding with if it is not valid JSON please?

    Allan

  • savanphrsavanphr Posts: 5Questions: 1Answers: 0

    I'm not sure to understand but I opened the debug on Chrome and can see the xhr response :
    connect database : uat{"data":[{"DT_RowId":"row_1","autor":"Rob","date":"Thu, 1 Feb 18","desc":"Insert 1","status":"On going","comment":"ok"},{"DT_RowId":"row_2","autor":"Steve","date":"Wed, 7 Feb 18","desc":"test insert 2","status":"On Going","comment":"Running ?"},{"DT_RowId":"row_3","autor":"Bob","date":"Thu, 8 Feb 18","desc":"Insert 3","status":"Testing","comment":"marre"},{"DT_RowId":"row_4","autor":"","date":null,"desc":"","status":"","comment":""}],"options":[],"files":[]}

    I see no error :

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin
    Answer ✓

    "connect database : uat"

    That part is making the JSON returned by the server invalid. Have you added an echo or print somewhere that shows the name of the database being connected to?

    Allan

  • savanphrsavanphr Posts: 5Questions: 1Answers: 0

    Thank you very much Allan. It works now.
    There was an echo in query.php : echo "connect database : " . $db;
    It was generated by the generator, It's good to know and comment the line.

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    Could you confirm which query.php file please? I've just had a look, but I couldn't see that string anywhere.

    Thanks,
    Allan

  • savanphrsavanphr Posts: 5Questions: 1Answers: 0

    I found It in this directory :
    php\lib\Database\Driver\Sqlserver\Query.php

  • savanphrsavanphr Posts: 5Questions: 1Answers: 0

    line 63 for me.

This discussion has been closed.