Editor not posting using POST or GET

Editor not posting using POST or GET

tklinetkline Posts: 6Questions: 0Answers: 0
edited August 2012 in Editor
I just purchased Editor and am having an odd problem. How do I see the POSTed data sent to my PHP script?

I have a simple PHP script I use for debugging - it simply var_exports $_REQUEST, $_POST, and $_GET to a file so I can debug the data sent from the client.

When I load a page using just a datatable, I can see the request from the datatable as a POST or GET depending how I set sServerMethod.

When I load a page using a datatable with Editor, I cannot figure out how to see the data sent to the server. All I see is a "SpryMedia_JSDoc" cookie sent and a get request of "_ = 1346253386732".

Here is my editor init code:

[code]
var editor; // use a global for the submit and return data rendering in the examples
var datatable;

$(document).ready(function() {
editor = new $.fn.dataTable.Editor( {
"ajaxUrl": "utility_scripts/write_to_log.php",
"domTable": "#example",
"ajax": function ( method, url, data, successCallback, errorCallback ) {
$.ajax( {
"type": "GET",
"url": url,
"data": data,
"dataType": "json",
"success": function (json) {
successCallback( json );
},
"error": function (xhr, error, thrown) {
errorCallback( xhr, error, thrown );
}
} );
},
"fields": [{"label":"ID", "name":"truck_id", "type": "readonly", "ipOpts": "", "default": ""},{"label":"Name", "name":"name", "type": "text", "ipOpts": "", "default": ""},{"label":"Value", "name":"insurance_value", "type": "text", "ipOpts": "", "default": ""},{"label":"Updated", "name":"insurance_value_updated", "type": "date", "ipOpts": "", "default": ""}] } );

datatable = $('#example').dataTable( {
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"sDom": '<"H"Tlfr>t<"F"ip>',
"sAjaxSource": "utility_scripts/write_to_log.php",
"sServerMethod": "GET",
"aoColumns": [{ "mDataProp": "truck_id" },{ "mDataProp": "name" },{ "mDataProp": "insurance_value" },{ "mDataProp": "insurance_value_updated" }],
"oTableTools": {
"sRowSelect": "single",
"aButtons": [
{ "sExtends": "editor_create", "editor": editor },
{ "sExtends": "editor_edit", "editor": editor }
]
}
} );
} );
[/code]


By setting the type to GET in the ajax function in the editor call and setting sServerMethod to GET in the dataable call, shouldn't I be able to see the data request in $_GET in my php script? The reference on the web site says data is sent via POST by default, but I can not see how that works, either.

This first came up as I tried to find a way to get the value of dbTable on the server when I set it on the client, but I could not figure out how to access it in PHP.

Please, any insight as to what I am missing would be most appreciated!

Thanks
Tom

Replies

  • tklinetkline Posts: 6Questions: 0Answers: 0
    The strange thing is the provided PHP classes to interact with the database work. How are they receiving the data passed from the client datatable in the request?
  • allanallan Posts: 51,440Questions: 1Answers: 7,758 Site admin
    Hi Tom,

    Thanks for your purchase of Editor!

    When you load the page, DataTables will make a GET request to sAjaxSource. Editor comes into play later, when you submit an Editor form (be it creating a new record, or editing an existing one). So you need to click the create / edit button and submit the form - when you do that, you should see an XHR being made (use Firebug or Webkit's Inspector to see that - you will also be able to see the parameters being sent).

    From there, your variables will be available in $_POST or $_GET as required.

    Its worth noting that if you just want to override Editor's `ajax` method to make the request GET, rather than POST, you can just use `ajaxUrl` like this:

    [code]
    "ajaxUrl": "GET utility_scripts/write_to_log.php"
    [/code]

    i.e. the method is put at the start of the URL - a bit like an actual HTTP request.

    Having said that, I would recommend using POST for submitting forms!

    Regards,
    Allan
  • tklinetkline Posts: 6Questions: 0Answers: 0
    This is really bizarre.

    I am using the above JS to load the table. This is the php script that sends back the data:

    [code]
    <?php
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    $log = "REQUEST: ".var_export($_REQUEST, true)."\n\n";
    $log .= "POST: ". var_export($_POST, true)."\n\n";
    $log .= "GET: ". var_export($_GET, true)."\n\n";

    file_put_contents("utility_scripts/write_to_log.log","\n\n".date('c')." ***************************************************\n", FILE_APPEND);
    file_put_contents("utility_scripts/write_to_log.log",$log, FILE_APPEND);

    // DataTables PHP library
    include( "php_common/datatables/DataTables.php" );

    // Alias Editor classes so they are easy to use
    use DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Join,
    DataTables\Editor\Validate;


    // Build our Editor instance and process the data coming from _POST
    Editor::inst( $db, 'trucks', 'truck_id' )
    ->fields(
    Field::inst( 'truck_id' ),
    Field::inst( 'name' ),
    Field::inst( 'insurance_value' ),
    Field::inst( 'insurance_value_updated' ))
    ->process( $_POST )
    ->json();




    ?>
    [/code]

    The logging lines at the very top dump $_REQUEST, $_POST, and $_GET into a file so I can see them. This is what I get when the table loads:
    [code]
    2012-08-29T13:05:34-04:00 ***************************************************
    REQUEST: array (
    'SpryMedia_JSDoc' => '1-1-true',
    'GCV_SIS' => '5eddd73a2c12e18a65bf2ea3078ebdd9',
    )

    POST: array (
    )

    GET: array (
    )

    [/code]

    Here is what confuses me: The script continues and the ->process($_POST) line executes and returns data to the client! The table works! Why can't I see or access the $_POST values?

    I realize this may be more a PHP question, but I have never run into this before. When I don't use the classes provided with Editor, I can read $_POST. Honestly, I'm not well versed in using classes and namespaces with PHP. Could this be a namespace issue, even though I thought the "superglobals" in PHP were available in every scope. I am using PHP 5.3.5 on the server.
  • tklinetkline Posts: 6Questions: 0Answers: 0
    When I edit a row, it updates the database correctly.

    I then see this in my log file:

    [code]
    2012-08-29T13:18:54-04:00 ***************************************************
    REQUEST: array (
    'action' => 'edit',
    'table' => '',
    'id' => 'row_5',
    'data' =>
    array (
    'truck_id' => '5',
    'name' => 'Red',
    'insurance_value' => '12000.00',
    'insurance_value_updated' => 'Thu, 25 Jan 2018',
    ),
    'SpryMedia_JSDoc' => '1-1-true',
    'GCV_SIS' => '5eddd73a2c12e18a65bf2ea3078ebdd9',
    )

    POST: array (
    )

    GET: array (
    'action' => 'edit',
    'table' => '',
    'id' => 'row_5',
    'data' =>
    array (
    'truck_id' => '5',
    'name' => 'Red',
    'insurance_value' => '12000.00',
    'insurance_value_updated' => 'Thu, 25 Jan 2018',
    ),
    )
    [/code]

    It seems the initial data table load request is not available to me. I have tried it by setting the request type to GET and POST with no difference.
  • allanallan Posts: 51,440Questions: 1Answers: 7,758 Site admin
    > Here is what confuses me: The script continues and the ->process($_POST) line executes and returns data to the client! The table works! Why can't I see or access the $_POST values?

    That's correct - there are two 'programs' making a call to your script here - both DataTables and Editor. When you load the page, DataTables makes a request to the server that does not have any POST parameters - just GET (a jQuery anti-cache parameter, and something from JSDoc). The load does not POST any data - there is nothing to send at that point!

    The Editor class realises that there is no data being sent in POST, so it assumes that the request is to get data, and it fires back the data.

    This is the block from the 'process' method that makes that decision:

    [code]
    if ( !isset($data['action']) ) {
    /* Get data */
    $this->_out['aaData'] = $this->_get();
    }
    // ... otherwise data is being sent - process it
    [/code]

    Allan
  • tklinetkline Posts: 6Questions: 0Answers: 0
    I see what is going on now. I was comparing my editor scripts with another datatable example script I had written a few weeks ago. That script uses the "bServerSide": true, parameter, which causes the JS to send a GET or POST request on initial table load. My script using editor above, did not have the "bServerSide": true paramater, so it was just requesting the page, which caused the process method to just send the table data.

    ah-ha! It helps to compare apples to apples!

    Thanks for your time, Allan.
  • allanallan Posts: 51,440Questions: 1Answers: 7,758 Site admin
    Ah - yes that would do it :-). Good to hear we got to the bottom of it. No magic involved in the making of DataTables / Editor. Just pixie-dust ;-)

    Allan
This discussion has been closed.