Search code examples
phpjquerypostdatatablesserver-side

Jquery Datatables - Can't Access Server Side Custom Parameters, fnServerParams, POST method using PHP


http://legacy.datatables.net/usage/server-side <--- reference page

Hey all,

I've been writing some server side scripts using databases with great success. Right now, i'm trying to get the fnServerParams method to send something to PHP.

$(document).ready(function() {
  var my_inventory = $('#my_inventory').dataTable({
    "iDisplayLength": 10,
    "bprocessing": true,
    "bServerSide": true,
    "sAjaxSource": "scripts/my_inventory.php",
    "sServerMethod": "POST",

    "fnServerParams": function(aoData) {
      var character = $('#my_inventory_character').val();
      console.log(character);
      aoData.push({
        "name": "more_data",
        "character": character
      });
    }
  })
);  

My character variable works fine. The script loads the data and client properties fine. It does not see the custom properties.

Typically I access the sent client data properties from the below list via PHP - using $_POST['variablenamehere'].

"dl.dropboxusercontent.com/u/44445906/capture/variables.PNG" (Can't link more then twice)

However, when using the method as described - i'm unable to access $_POST['character'] or $_POST['name']. Infact, empty /null.

https://dl.dropboxusercontent.com/u/44445906/capture/sever.PNG

TLDR:

Trying to access custom parameters using POST and PHP. Can only seem to access standard variables using this method. How have you solved this?

Without using POST, the same thing happens using $_GET['variablename'] - standard available.. custom not.


Solution

  • For those running into this issue. You can access the variables by ensuring you are using datatables 1.10 or later and then in PHP for the above example using $_GET['more_data'].

    The 'name' attribute in datatables, specifies the actual name of the variable being sent to the server side.

    You are not sending something with the "name" and value of "more_data". In the above, you're sending something named "more_data" with the value of 'character'. Each new variable you want to send requires another array with 'name' and 'value' properties.