0

So, recently I have been dabbling a bit in cakephp but I have run into an issue I just can't seem to solve. I suspect it's something simple I'm missing (as most cases). The premise of what I'm trying to do is somewhat self-explanatory in the title: I have a view (my add.ctp) within this add I have a form created with the form helper.

I have multiple selects, the first select is a list of companies. Once the company has been selected, I now want to update the next select based on the selected value using jquery and ajax sending a get request to the controller.

The AJAX request completes successfully however, I cannot seem to access the desirable returned data (a list of projects that belong to the selected company).

To be clear I'm trying to return an array in the success callback

I have read a lot and searched around, but I feel that maybe examples are lacking for v3 cakephp.

Some of my code :

Controller

   $projectphase = $this->Projectphases->newEntity();
   $data = ['content' => 'hi', 'error' => 'nothing found'];
   $projects = array('0' => 'Select something');
   if($this->request->is('ajax')){

        $company_id = $this->request->data('company_id');
        $projects = $this->Projectphases->Projects->find('list', ['limit' => 2, 'conditions' => ['company_id' => $company_id]]);
        $arr = array();
        $arr = $this->chainedProjects($company_id);
        $data = ['content' => 'hello', 'error' => ''];
        $this->set(compact('projects', 'data'));
        $this->set('_serialize', ['projects', 'data']);

   }elseif($this->request->is('post')){

       debug("hit post");
       die();
   }

Public function chainedProjects

    $projects = $this->Projectphases->Projects->find('list', ['limit' => 2, 'conditions' => ['company_id' => $id]]);
    $projs = $projects->toArray();
    $values = array();
    $x = 0;
    foreach ($projs as $key => $value) {
         $values[$x] = "<option value='$key'>$value</option>";
         $x++;
    }
    return $values;

Javascript [jquery ajax]

    $(document).ready(function(){

    $('#company').change(function () {
        $company_id = $('#company').val();
        var data = {
            'type' : 'dropdown',
            'company_id' : $company_id
        };
        $.ajax({
            type        : 'GET',
            //url       : 'delegate1/projectphases/add',
            data        : data,
            encode      : true,
            beforeSend: function(xhr){
                xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
            },
            success: function(response){
                console.log('success');
                console.log(response.content);
                console.log(response.message);

                //console.log(data.response);
                //console.log(result.content);
                console.log(response);
            }       
        }).done(function(){
            console.log('done');
        })
    });
    })

Jquery 2.1.3

Any help whatsoever will be appreciated!

Neal
  • 573
  • 3
  • 16
  • possible duplicate of [Can't parse and return ajax string to jquery variable](http://stackoverflow.com/questions/31360437/cant-parse-and-return-ajax-string-to-jquery-variable) – vinayakj Jul 25 '15 at 08:26
  • `The AJAX request completes successfully however, I cannot seem to access the desirable returned data (a list of projects that belong to the selected company).` , you need to access returned data in `done/success` callback only – vinayakj Jul 25 '15 at 08:27
  • Yes, I understand that only in the callback you are able to access modified data. That is why I'm calling console.log multiple times in the success The .done is for testing, apologies should have been less ambiguous. – Neal Jul 25 '15 at 08:38
  • alright, `console.log` is ok but are you using response from callback only?, I dont know cakePHP much – vinayakj Jul 25 '15 at 08:41
  • Something like `done(function(data){ fillDropdown(data); })` – vinayakj Jul 25 '15 at 08:42
  • `To be clear I'm trying to return an array in the success callback` , you cant return data from asynchronous call to use it immediately on next line, to use data returned you need to have callback function to do that – vinayakj Jul 25 '15 at 08:43
  • @vinayakj I understand it being async etc, but I suppose I could be going about this the wrong way. I am perhaps not understanding fully how the callback behaves, I did try to set async to false but it is deprecated and quite honestly not what I want to do. I will try to use a function within the .done event with a callback. Thanks for the help, will come back here with updates :p – Neal Jul 25 '15 at 08:47
  • ok.. read this one see if it helps, [Asynchronous callback](http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – vinayakj Jul 25 '15 at 08:50

0 Answers0