Error: Undefined index for 'draw', 'start' - parameters in PHP
Error: Undefined index for 'draw', 'start' - parameters in PHP
Hi,
i'm struggling for a very long time now with this problem and googling the sh*t out of myself in order to solve it.
I'm trying to get a connection to a mySQL - database which should load the data into the datatable and show it. When i'm running the program i get the message "Undefined index: draw in /Applications/XAMPP/xamppfiles/htdocs/kpibib/ajaxfile.php on line 8. I read a lot of times that you should change the "type" parameter in the JS - code to "POST". This is what i've did but when i'm checking on "Network" in Chrome i see that the Request type is still on "GET". So this isn`t working for me. How can i solve this issue?
This is my code:
The JS - Code:
$("#kpitable").DataTable({
"processing": true,
"serverSide": true,
"ajax": {
"type": "POST",
"url": 'ajaxfile.php'
},
'columns':[
{ "data": 'KPINUMBER'},
{ "data": 'KPINAME'},
{ "data": 'REPAREA'},
{ "data": 'KPITYPE'},
{ "data": 'REPUNIT'},
{ "data": 'RESPDEP'},
{ "data": 'BASEORCALC'},
{ "data": 'FORMULA'},
{ "data": 'DESCR'},
{ "data": 'CONTACTPER'}
]
});
and this is my server-side script written in PHP:
<?php
php
?>
include 'connect.php';
##Read values
$draw = $_POST["draw"];
$row = $_POST["start"];
$rowperpage = $_POST["length"];
$columnIndex = $_POST["order"][0]["column"];
$columnName = $_POST["columns"][0]["data"];
$columnSortOrder = $_POST["order"][0]["asc"];
$searchValue = $_POST["search"]["value"];
$searchArray = array();
##search
$searchQuery = " ";
if ($searchValue != ''){
$searchQuery = "AND (KPINUMBER LIKE :KPINUMBER OR KPINAME LIKE :KPINAME OR REPAREA LIKE :REPAREA OR KPITYPE LIKE :KPITYPE OR REPUNIT LIKE :REPUNIT OR RESPDEP LIKE :RESPDEP
OR BASEORCALC LIKE :BASEORCALC OR FORMULA LIKE :FORMULA OR DESCR LIKE :DESCR OR CONTACTPER LIKE :CONTACTPER) ";
$searchArray = array('KPINUMBER'=>"%searchValue%",
'KPINAME'=>"%searchValue%",
'REPAREA'=>"%searchValue%",
'KPITYPE'=>"%searchValue%",
'REPUNIT'=>"%searchValue%",
'RESPDEP'=>"%searchValue%",
'BASEORCALC'=>"%searchValue%",
'FORMULA'=>"%searchValue%",
'DESCR'=>"%searchValue%",
'CONTACTPER'=>"%searchValue%"
);
}
##Total number of records without filtering
$stmt = $db->prepare("SELECT COUNT(*) AS allcount FROM kpidata");
$stmt->execute();
$records = $stmt->fetch();
$totalRecords = $records['allcount'];
##Total number of records with filtering
$stmt = $db->prepare("SELECT COUNT(*) AS allcount FROM kpidata WHERE 1 ".$searchQuery);
$stmt->execute($searchArray);
$records = $stmt->fetch();
$totalRecordswithFilter = $records['allcount'];
##Fetch records
$stmt = $db->prepare("SELECT * FROM kpidata"); /*WHERE 1".$searchQuery." ORDER BY ".$columnName." ".$columnSortOrder." LIMIT :limit,:offset");*/
##bind VALUES
foreach($searchArray as $key=>$search){
$stmt->bindValue(':'.$key, $search,PDO::PARAM_STR);
}
$stmt->bindValue(':limit', (int)$row, PDO::PARAM_INT);
$stmt->bindValue(':offset', (int)$rowperpage, PDO::PARAM_INT);
$stmt->execute();
$kpirecords = $stmt->fetchAll();
$data = array();
foreach ($kpirecords as $row) {
$data[] = array(
"KPINUMBER"=>$row['KPINUMBER'],
"KPINAME"=>$row['KPINAME'],
"REPAREA"=>$row['REPAREA'],
"KPITYPE"=>$row['KPITYPE'],
"REPUNIT"=>$row['REPUNIT'],
"RESPDEP"=>$row['RESPDEP'],
"BASEORCALC"=>$row['BASEORCALC'],
"FORMULA"=>$row['FORMULA'],
"DESCR"=>$row['DESCR'],
"CONTACTPER"=>$row['CONTACTPER']
);
}
##Response
$response = array(
"draw" => intval($draw),
"recordsTotal" => $totalRecords,
"recordsFiltered" => $totalRecordswithFilter,
"aaData" => $data
);
echo json_encode($response);
Answers
There isn't anything that stands out as a reason why Datatables isn't sending a POST request.
Using type POST in this example works:
https://datatables.net/examples/server_side/post.html
Maybe try clearing your browser's cache?
In order to help debug we will need a link to your page or a test case replicating the issue.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin