1

I have the following code to create a datatable.

Instead of return json result from my php file, it returns the php file itself.

I checked the php file on localhost page and it seems the php file is working fine:

{"draw":0,"recordsTotal":115,"recordsFiltered":115,"data":[{"chr":"chr1","pos":631704},{"chr":"chr1","pos":631714},{"chr":"chr1","pos":631848},{"chr":"chr1","pos":632344},{"chr":"chr1","pos":632461},{"chr":"chr1","pos":633015},{"chr":"chr1","pos":633143},{"chr":"chr1","pos":633300},{"chr":"chr1","pos":633364},{"chr":"chr1","pos":633839},{"chr":"chr1","pos":633852},{"chr":"chr1","pos":633860}............

Could someone help me with it?

I tried this answer by adding

PHP code is not being executed, but the code shows in the browser source code

LoadModule php_module "C:/xampp/php/php8apache2_4.dll"
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps

but still didn't work.

`<script>
    $(document).ready(function() {
        $('#example').DataTable( {
            "processing": true,
            "serverSide": true,
            "ajax": {url : "example.php",
                     type: "GET",
                    },
            "columns": [
{ "data": 'chr'},
{ "data": 'pos'}
]
        } );
    } );
</script>`

Thank you.

Jie Jiang
  • 19
  • 4

1 Answers1

0

Try adding dataType: "json"... I'm also 6 months new with Javascript, let me know if it doesn't work and I'll try something else. I've been working with php for 20 years.

<script>
   $(document).ready(function() {
            $('#example').DataTable( {
                "processing": true,
                "serverSide": true,
                "ajax": {url : "example.php",
                         type: "GET",
                         dataType:"json",
                        },
                "columns": [
    { "data": 'chr'},
    { "data": 'pos'}
    ]
            } );
        } );
    </script>

In your PHP script... i'm not familiar with SSP class but couldn't you do something like this? This is how I return json from php to javascript in my game.

print json_encode(array("food"=>$_SESSION['food'],"wood"=>$_SESSION['wood'],"gold"=>$_SESSION['gold'],"stone"=>$_SESSION['stone']));

and then use data.food in javascript inside function(data){ after ajax call

  • I tried to add datatype but it still didn't work. It feels like the php may not be excuted properly. – Jie Jiang Apr 29 '22 at 04:05
  • whats the php code part of it? – Jayy Monster Apr 29 '22 at 04:12
  • this entire file will show up in the network---reponse of the html file 'chr', 'dt' => 'chr' ), array( 'db' => 'pos', 'dt' => 'pos'), ); // SQL server connection information $sql_details = array( 'host' => 'localhost', 'user' => 'root', 'pass' => '', 'db' => 'directrmdb' ); require( './php/ssp.class.php' ); echo json_encode( SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns) ); ?> – Jie Jiang Apr 29 '22 at 04:20
  • checking it out, 1 min – Jayy Monster Apr 29 '22 at 04:26
  • check out my updated answer, if it doesn't work I'll look more into it. – Jayy Monster Apr 29 '22 at 04:35
  • I am sorry I am new to both Javascript and php. I don't quite understand how to use data.food in ajax call.....do i just replace data : XXX with data.food? it seems it doesn't work... – Jie Jiang Apr 29 '22 at 14:46