Ajax failed to pass data to php server side processing
Ajax failed to pass data to php server side processing
Link to test case:
Debugger code (debug.datatables.net): https://debug.datatables.net/ilazub
Error messages shown:
Description of problem:
I want to make a ajax call to process the table on server side. If I don't pass any data by ajax to the server side, the code works just fine and the table can be updated from the server side data. But when I want to pass some parameters to server side for filter, it doesn't work. My php gets empty array() for both $_GET and $_POST. I have tried different set up with ajax (e.g contentType, async etc)but none of them worked. I don't know whehter it is because of the ajax or the php configuration.
The datatable:
$(document).ready(function() {
$("#example").DataTable({
"destroy": true,
"paging": true,
"serverSide": true,
"hover":true,
"utoWidth":true,
"processing":true,
"searching": false,
"lengthMenu": [10,15,20,25],
"order": [[9, 'desc']],
"info" : true,
"sScrollX":true,
columns: [
{data: "dRid"},
{data: "chromosome"},
{data: "position"},
{data: "strand"},
{data: "cell_line"},
{data: "modification"},
{data: "geneId"},
{data: "species"},
{data: "rbp"},
{data: "mirna"},
{data: "splicingsite"},
{data: "snp"}
],
ajax:({
type:"GET", // POST also tried
url:"./php/search_test.php",
datType: 'JSON',
data: {"species": species}
})
} );
} );
<?php
ini_set ('memory_limit', '2048M');
$sql_details = array(
'host' => 'localhost',
'user' => 'root',
'pass' => '',
'db' => 'directrmdb'
);
// DB table to use
$table = 'homo_sapiens';
//if I try this here: $species=$_GET(or POST)["species"]; echo $species;It
//returns: Warning: Undefined array key "species". Without it, the code worked fine and the table can be returned to html page.
// Table's primary key
$primaryKey = 'dRid';
// column
$columns = array(
array( 'db' => 'dRid', 'dt' => 'dRid' ),
array( 'db' => 'chr', 'dt' => 'chromosome' ),
array( 'db' => 'pos', 'dt' => 'position' ),
array( 'db' => 'strand', 'dt' => 'strand' ),
array( 'db' => 'cell_line', 'dt' => 'cell_line' ),
array( 'db' => 'modification', 'dt' => 'modification' ),
array( 'db' => 'geneId', 'dt' => 'geneId' ),
array( 'db' => 'species', 'dt' => 'species' ),
array( 'db' => 'RBP_number', 'dt' => 'rbp' ),
array( 'db' => 'miRNA_target_number', 'dt' => 'mirna' ),
array( 'db' => 'SplicingSite_number', 'dt' => 'splicingsite' ),
array( 'db' => 'SNP_number', 'dt' => 'snp' ),
array( 'db' => 'species', 'dt' => 'species' )
);
// Include SQL query processing class
require( 'ssp.class.php' );
echo json_encode(
SSP::complex( $_GET, $sql_details, $table, $primaryKey,$columns)
);
?>
Could someone help me with it? Thank you.
Answers
See this Add Parameters
I want to pass static paramters rathe than a function. I suppose I shall use format like data:{"species" : speicies} ?
Assuming your
speicies
variable doesn't change value, then yes that will work just fine (documentation for it is here:ajax.data
). If it can change value and you need to update the data based on that, then yes, you must use a function.Allan
I tried both function and data: {"species": species} but neither works. The problem is the ajax call seems working because I do have a table displayed on the page with server side data. However, no matter which method I use, I cannot pass any data to the php file. $_POST and $_GET are always empty.
Where are you setting the Javascript variable
species
? I don't see it anywhere in the above code. I'm wondering if it is actually undefined at that point?If you can give me a link to your page showing the issue, I could say for sure.
Thanks,
Allan
Hi Allan,
The part with 'species':
I console.log(species); before the datatable function and it shows 'homo_sapiens' in the console. So I think it is defiend without problem. Thx.
To progress this, it would really help to see your page as Allan asked. Could you PM the details to him (or me), please, and we can take a look,
Colin
Hi Allan and Colin,
I sent a message to Allan with the code. Could you please help me to check it? Thanks!
Thanks for the plnkr link. The request there is including your parameter - from the Firefox network console:
You should be able to access that in your PHP script as
$_GET['species']
.Allan
Hi Allan,
I managed to get the data from mySQL but my php still have the error message 'Undefined array key "species"'. I guess I just leave it as it is as long as the results can return.
Thank you very much for your kindly help!
Can you show me the full PHP script please?
Allan