POST variables not being passed to server side
POST variables not being passed to server side
I am having a hard time getting the serverside functionality to work correctly. I have been searching and trying to get this to work for about 2 days now... I have tried EVERYTHING I have found.
When I set the type to "POST", I don't get any variables passed over, and therefore, I cannot query SQL correct because I don't know start and length. When I remove POST or set it to GET, I get a funny object that I can parse and get the correct values, but I don't think it's supposed to be that way.
var contactTable = $('#contactList').DataTable({
processing: true,
serverSide: true,
ordering: false,
searching: {"regex": true},
lengthMenu: [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]],
pageLength: 50,
ajax: {
"type": "POST",
"url": "api/get-contacts.php",
// "dataSrc":"",
"dataType": "json",
"contentType": 'application/json; charset=utf-8'
},
stateSave: true,
columns: [
{ "data": "check"},
{ "data": "id" },
{ "data": "first_name" },
{ "data": "last_name" }
],
columnDefs: [
{
"className": 'select-checkbox',
"targets": 0
},
{
"visible": false, "targets": [1, 5, 9, 10, 11, 13]
}
],
select: {
style: 'os',
selector: 'td:first-child'
},
order: [[1, 'asc']]
});
I have tried to place this in data, but it doesn't seem to help...
"data": function (data) {
// Grab form values containing user options
var form = {};
$.each($("form").serializeArray(), function (i, field) {
form[field.name] = field.value || "";
});
// Add options used by Datatables
var info = (contactTable == null) ? { "start": 0, "length": 50 } : contactTable.page.info();
$.extend(form, info);
return JSON.stringify(form);
},
All I want is to be able to access the variables on the serverside so I can make the correct SQL queries.
Can someone please help?
This question has an accepted answers - jump to answer
Answers
Not sure what you are using for your server script. Whatever it is PHP, Python, etc you will need to setup the script to accept POST requests then parse the parameters.
Here is a PHP server side processing example with POST. Take a look at the Server-side script tab.
https://datatables.net/examples/server_side/post.html
Kevin
I apologize... I knew I was forgetting something.
This is the top of my PHP page for serverside
I get errors on this page saying that 'start', 'length', etc... are all undefined. When I dump $_REQUEST and $_POST, there is nothing.
That example doesn't help me much because it doesn't show how to access the variables in the PHP page. I feel like this is something simple that I am just over looking...
Maybe it's the ssp.class.php? I don't have that... Will that help access the variables?
I found it here https://github.com/DataTables/DataTables/blob/master/examples/server_side/scripts/ssp.class.php but I fail to see how that will help me. I just need access to the variables, not my DB. I have my queries already created.
So, I ended up stripping the whole thing down and now it works. I am not sure what I was doing.
datatables.php
serverside.php
Thank you for your help.