0

I started with PHP/Ajax/javascript 2 days ago. Currently I try out server side processing on datatables.. I've two files on my webserver. One to display and one to process files.

Here the code:

<!DOCTYPE html>
<html>
  <head>
    <title>Person Information</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.min.css"/>
    <script type="text/javascript" language="javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>
    <script type="text/javascript" language="javascript" src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
<script>
$(document).ready(function() {
    $('#example').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax": "/server_processing.php"
    } );
} );
</script>
  </head>



<table id="example" class="display" style="width:100%">
<thead><tr>
    <th title="Field #1">playerid</th>
    <th title="Field #2">playername</th>
    <th title="Field #3">age</th>
    <th title="Field #4">nation</th>
    <th title="Field #5">teamid</th>
    <th title="Field #6">team</th>
    <th title="Field #7">overall</th>
    <th title="Field #8">potential</th>
</tr></thead>
</table>


<?php
 $table = 'example';
$primaryKey = 'playerid';
$columns = array(
    array( 'db' => 'playerid', 'dt' => 0 ),
    array( 'db' => 'playername',  'dt' => 1 ),
    array( 'db' => 'age',   'dt' => 2 ),
    array( 'db' => 'nation',     'dt' => 3 ),
    array( 'db' => 'teamid',     'dt' => 4 ),
    array( 'db' => 'team',     'dt' => 5 ),
    array( 'db' => 'overall',     'dt' => 6 ),
    array( 'db' => 'potential',     'dt' => 7 )
);
$sql_details = array(
    'user' => 'myusername',
    'pass' => 'mypassword',
    'db'   => 'mydatabasename',
    'host' => 'localhost'
);
require( 'ssp.class.php' );
echo json_encode(
    SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);

I get this message: DataTables warning: table id=example - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1 I tried to debug it and found out that this adress get called:

http://webserver.com/server_processing.php?draw=1&columns%5B0%5D%5Bdata%5D=0&columns%5B0%5D%5Bname%5D=&columns%5B0%5D%5Bsearchable%5D=true&columns%5B0%5D%5Borderable%5D=true&columns%5B0%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B0%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B1%5D%5Bdata%5D=1&columns%5B1%5D%5Bname%5D=&columns%5B1%5D%5Bsearchable%5D=true&columns%5B1%5D%5Borderable%5D=true&columns%5B1%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B1%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B2%5D%5Bdata%5D=2&columns%5B2%5D%5Bname%5D=&columns%5B2%5D%5Bsearchable%5D=true&columns%5B2%5D%5Borderable%5D=true&columns%5B2%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B2%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B3%5D%5Bdata%5D=3&columns%5B3%5D%5Bname%5D=&columns%5B3%5D%5Bsearchable%5D=true&columns%5B3%5D%5Borderable%5D=true&columns%5B3%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B3%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B4%5D%5Bdata%5D=4&columns%5B4%5D%5Bname%5D=&columns%5B4%5D%5Bsearchable%5D=true&columns%5B4%5D%5Borderable%5D=true&columns%5B4%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B4%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B5%5D%5Bdata%5D=5&columns%5B5%5D%5Bname%5D=&columns%5B5%5D%5Bsearchable%5D=true&columns%5B5%5D%5Borderable%5D=true&columns%5B5%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B5%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B6%5D%5Bdata%5D=6&columns%5B6%5D%5Bname%5D=&columns%5B6%5D%5Bsearchable%5D=true&columns%5B6%5D%5Borderable%5D=true&columns%5B6%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B6%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B7%5D%5Bdata%5D=7&columns%5B7%5D%5Bname%5D=&columns%5B7%5D%5Bsearchable%5D=true&columns%5B7%5D%5Borderable%5D=true&columns%5B7%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B7%5D%5Bsearch%5D%5Bregex%5D=false&order%5B0%5D%5Bcolumn%5D=0&order%5B0%5D%5Bdir%5D=asc&start=0&length=10&search%5Bvalue%5D=&search%5Bregex%5D=false&_=1581016871964

but there is no answer. Any ideas how to fix?

  • Most likely the response does not contain valid JSON data. Can you provide the contents of `server_processing.php`? – Christos Lytras Feb 06 '20 at 19:31
  • The content of server_processing is the php script a bit down.. https://pastebin.com/JzCD1V7x – Slim Ontario Feb 06 '20 at 20:02
  • You should add a JSON header. Is this one file `server_processing.php`? HTML and PHP? – Christos Lytras Feb 06 '20 at 20:14
  • I just added a "header('Content-Type: application/json');" to the php (server_processing.php). And yeah its PHP. – Slim Ontario Feb 06 '20 at 21:19
  • If it's the same file then it will not work because it will output the HTML every time at the beginning. You need a separate file to return the JSON data and do just that. – Christos Lytras Feb 06 '20 at 21:21
  • I failed when entering the code above. I have 2 files. 1 html (called "test.htm" with the content : https://pastebin.com/29hKDTkX) and 1 php file (called "server_processing-php) which has the json stuff in it. Also adding the json-head didn't helped for me. I still get the error. Btw thanks for your help!! – Slim Ontario Feb 06 '20 at 21:23
  • You should use the network tab to see what the AJAX call returns. On windows hit F12 and then click the Network tab. Then refresh or trigger the AJAX call, you should see the request call. If you click on it you'll see the response, what it returns to the browser. There you'll see if it returns valid JSON or some other data. – Christos Lytras Feb 06 '20 at 21:27
  • It returns nothing... When I enter the test.htm the following adress gets called: https://pastebin.com/KTtK0dY4 .. but there is no answer to that. Also on my webserver-log I can see that there is a GET request to test.htm and after that to server_processing.php – Slim Ontario Feb 06 '20 at 21:32
  • That means there is an error inside your PHP script, but your PHP settings most likely have errors disabled. You should enable PHP errors in PHP settings and then use [Insomnia REST Client](https://insomnia.rest/) to debug your server script. – Christos Lytras Feb 06 '20 at 21:39
  • It works now!! Thanks for your help Christos! For everyone else who want to know how I solved it... Encoding. >.< Just use the utf-function: https://stackoverflow.com/questions/19361282/why-would-json-encode-return-an-empty-string – Slim Ontario Feb 07 '20 at 06:58

0 Answers0