Search code examples
jqueryajaxcakephpcakephp-2.4.7

Manage AJAX request data in a CakePHP controller


I'm trying to access an AJAX request's data from a CakePHP controller, but failing to understand where is the form's data once I'm in the controller.

Inside my view, between form tags, I have the following code:

echo $this->Js->submit(
    'Proceed',
    array(
    'url' => array(
        'controller' => 'json',
        'action' => 'itemSelection'
    ),
    'dataType' => 'json',
    'id' => 'proceed',
    'success' => 'onSuccessFunct( data )'
));

This results i the following code:

$(document).ready(function (){
    $("#proceed").bind("click", function(event){
        $.ajax({
            data:$("#proceed").closest("form").serialize(),
            dataType: "json",
            success:function (data, textStatus) {
                onSuccessFunct( data )
            },
            type:"post",
            url:"\/koro\/json\/itemSelection"
        });
        return false;
    });
});

This part seems to be working fine, the request is done on click, the success function is called, the problem is that I'm not understanding how to treat the form's data in the controller.

$this->request->data appears empty, and I'm not understanding where's the form data going to. If I'm not greatly misunderstanding JQuery, it should be parsed in a querystring manner, as shown in the .serialize() demo, but again, I'm failing to understand where is the serialized data stored once it's in the controller.

The code for the controller is just a loop through a table's rows, comparing form data with table data, and storing that in another variable which is then returned as JSON. An if($this->request->is('ajax')) is called before proceeding to the function's code, inside which there's the aforementioned loop, where there used to be an if which tested the form data to check that it's value was different to null and 0. I don't believe the controller code is really an issue.

To summarize:

  • Is there a default variable where CakePHP stores data from an AJAX request's serialized form?
  • If not, where is this data stored in regular AJAX requests || how can I redirect it to a variable of my choosing? (Sorry for this AJAX noob follow-up)

Needless to say, any comment/answer is welcome, thanks in advance

PD: The code was tested and worked fine when it was in it's pre-AJAX state (I was treating everything through post requests). In favor of order and usability I started migrating it to AJAX just yesterday.


Solution

  • Thanks to Kai for his friendly advice. Trying those debugs kept returning empty objects and arrays, $this->request->data was empty, so was $this->params->query, and almost every other possible option. It was very frustrating.

    I finally got it right emulating what posted here.

    The code:

    $data = $this->Js->get('#formName')->serializeForm(array('isForm' => true, 'inline' => true));
    $this->Js->get('#formName')->event(
        'submit',
        $this->Js->request(
            array('action' => 'itemSelection', 'controller' => 'json'),
            array(
                'data' => $data,
                'async' => true,    
                'dataExpression'=>true,
                'success' => 'onSuccessFunct( data )',
                'method' => 'POST'
            )
        )
    );
    

    I take it there was some problem with sending the data, and comparing the old code with the now functioning one I wonder would the old one work if I just added the 'dataExpression' => true line to it. Based on the Book's brief description, I'm guessing it would, though still not completely certain of the aforementioned description's meaning.

    I'd like to get further explanation if anyone should be willing to provide one, but I'm closing the question.