stateSaveCallback / stateLoadCallback with PHP

stateSaveCallback / stateLoadCallback with PHP

dragon013dragon013 Posts: 8Questions: 3Answers: 0

Hello,

I am trying to save the state of a datatable in a session via PHP but sadly I have do not have any success. Maybe someone can give me a helping hand.

Here the snippet from datatable:

"stateSaveCallback": function (settings, data) 
{  // Send an Ajax request to the server with the state object
   $.ajax( 
   {
      "url": "state_save.php",
      "data": data,
      "dataType": "json",
      "method": "POST"
   } );
}

Here the code of state_save.php (For testing, I just write it to a textfile):

$fp = fopen("test.txt","w");
fwrite($fp,var_dump($_POST));
fclose($fp);

The file is created, but it is empty.

When debugging the javascript, the variable "data" is filled with all values but it seems I am doing something wrong in the php part. Obviously reading from POST is not enough.

Since I was not able to find an example of a php code (maybe I was just blind), I would like to ask if someone is able to help me.

Thanks in adavance!

Dragon013

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,530Questions: 1Answers: 10,473 Site admin
    Answer ✓

    "method": "POST"

    In jQuery I think you want to use:

    "type": "POST"
    

    At the moment it is probably sending as a GET, which you can confirm with your browser's developer tools.

    Allan

  • dragon013dragon013 Posts: 8Questions: 3Answers: 0
    edited December 2014

    Hello Allan,

    I just tried

    "type": "POST"
    

    and now I was able to read the needed values. Thanks a lot!

    One question about method vs. type, in the example at https://datatables.net/reference/option/stateSaveCallback there is "method" used. What's the difference to "type" ?

    Thanks!

    Dragon013

  • allanallan Posts: 63,530Questions: 1Answers: 10,473 Site admin

    That's a bug in the documentation! Sorry about that! I've committed the fix and will deploy it to this site soon.

    Allan

This discussion has been closed.