简体   繁体   中英

PHP doesn't receive ajax post data

I have been struggling with it for a week...I want to use Ajax in datatables to pass some parameters to my PHP file for server side searching. But it seems my PHP file got nothing. But the datatable works just fine and the table can return from sever side processing and display on the html page correctly. However, var_dump( $_POST['species']) returns null.

 $(document).ready(function() { console.log(species); $("#table").DataTable({ "destroy": true, "serverSide": true, "processing": true, "paging": false, "searching": false, columns: [{ data: "d" }, { data: "c" }, { data: "p" } ], ajax: { type: "POST", data: { "species": s }, url: "./php/search_test.php" } }); });
 <div class="container text-center" id="tablecontainer" name="tablecontainer"> <table id="table" class="display nowrap row-border strip p-2" cellspacing="0" width="100%"> <thead class="thead-dark"> <tr> <th> <center>d</center> </th> <th> <center>C</center> </th> <th> <center>P</center> </th> </tr> </thead> </table> </div>

'''php

ini_set ('memory_limit',  '2048M');
$sql_details = array(
'host' => 'localhost',
'user' => 'root',
'pass' => '',
'db'   => 'xxxxx'
);

// DB table to use
$table = 'h';

//if I try this here: $s=$_POST["s"]; echo $s;It //returns: Warning: Undefined array key "s".

// Table's primary key
$primaryKey = 'd';

// column
$columns = array(
array( 'db' => 'd', 'dt' => 'd' ),
array( 'db' => 'c', 'dt' => 'c' ),
array( 'db' => 'p',  'dt' => 'p' ),


);

// Include SQL query processing class
require( 'ssp.class.php' );

echo json_encode(
SSP::complex( $_POST, $sql_details, $table, $primaryKey,$columns)
);

Could someone help me with it? Thanks.

Try this. Replace $_POST to $array

$array = json_decode(file_get_contents('php://input'), true); print_r($array);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM