Ajax Request Problem
Ajax Request Problem
Hi..
I've this code that work, but the file that receive the request, not receive the string $columns[$requestData['order'][0]['column']] and the $requestData['search']['value'].. Why?
`<?php
// Mi connetto al Database
$host = "localhost";
$db_user = "xxx";
$db_pass = "yyy";
$db = "zzz";
$mysqli = new mysqli($host, $db_user, $db_pass, $db) or die ("Impossibile connettersi al database" . $mysqli->connect_error);
$mysqli->set_charset("utf8");
// storing request (ie, get/post) global array to a variable
$requestData= $_REQUEST;
$columns = array(
// datatable column index => database column name
1 => 'phpbb32_users.username',
2 => 'g_giocatori.coins',
3 => 'g_giocatori.vittorie',
4 => 'g_giocatori.piuAlta',
5 => 'g_giocatori.totVinto',
6 => 'g_giocatori.totScommesso',
7 => 'g_giocatori.dataIscrizione'
);
// getting total number records without any search
$sql = "SELECT * ";
$sql.=" FROM g_giocatori";
$query=mysqli_query($mysqli, $sql) or die("FI_ajax_classifica.php: get employees");
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData; // when there is no search parameter then total number rows = total number filtered rows.
$sql = "SELECT g_giocatori.id, g_giocatori.coins, g_giocatori.vittorie, g_giocatori.piuAlta, g_giocatori.totVinto, g_giocatori.totScommesso, g_giocatori.dataIscrizione, phpbb32_users.user_id, phpbb32_users.username FROM g_giocatori INNER JOIN phpbb32_users ON g_giocatori.id = phpbb32_users.user_id WHERE 1=1";
if( !empty($requestData['search']['value']) ) { // if there is a search parameter, $requestData['search']['value'] contains search parameter
$sql.=" AND ( lower(g_giocatori.username) LIKE '%".strtolower($requestData['search']['value'])."%' ";
}
$query=mysqli_query($mysqli, $sql) or die("FI_ajax_classifica.php: get employees");
$totalFiltered = mysqli_num_rows($query); // when there is a search parameter then we have to modify total number filtered rows as per search result.
if($columns[$requestData['order'][0]['column']] != ''){
$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]." ".$requestData['order'][0]['dir']." LIMIT ".$requestData['start']." ,".$requestData['length']." ";
}else{
$sql.=" ORDER BY g_giocatori.coins DESC LIMIT ".$requestData['start']." ,".$requestData['length']." ";
}
/* $requestData['order'][0]['column'] contains colmun index, $requestData['order'][0]['dir'] contains order such as asc/desc */
$query=mysqli_query($mysqli, $sql) or die("FI_ajax_classifica.php: get employees");
$i = 0;
$data = array();
while( $row=mysqli_fetch_array($query) ) { // preparing an array
$nestedData=array();
$i++;
$nestedData[] = "<h4>$i</h4>";
$nestedData[] = "<h4>".$row['username']."</h4>";
$nestedData[] = "<h4>".$row['coins']."</h4>";
$nestedData[] = "<h4>".$row['vittorie']."</h4>";
$nestedData[] = "<h4>".$row['piuAlta']."</h4>";
$nestedData[] = "<h4>".$row['totVinto']."</h4>";
$nestedData[] = "<h4>".$row['totScommesso']."</h4>";
$nestedData[] = "<h4>".gmdate('d/m/Y', $row['dataIscrizione'])."</h4>";
$data[] = $nestedData;
}
$json_data = array(
"draw" => intval( $requestData['draw'] ), // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw.
"recordsTotal" => intval( $totalData ), // total number of records
"recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
"data" => $data // total data array
);
echo json_encode($json_data); // send data as json format
<?php >` ?>
Replies
Do you have
serverSide
enabled in you Datatables config?Kevin
Yes.. This is the code:
Scuse me but indentation not work here with javascript..!
EDIT: used triple (```) ticks to outline the code as noted in the Markdown docs.
I don't use the PHP scripts but my guess would be that you have defined the columns using names, not positions. However in the Datatables config you haven't defined the matching
columns.data
. Datatables is using arrays but your PHP is expecting object data - at least that is my guess.You can look at the
Server-side Processing
(array based) versus `Object data source (object based) examples here for comparison:https://datatables.net/examples/server_side/
Kevin
I'd suggest doing:
just after that line. That will show you everything that is being submitted by the client-side.
Allan
Solved.. The error is in double ( in the search