Pergunta

I'm trying to send ajax data back to the view, but its not working

Here is my controller:

    $this->autoRender = 0;
    $this->layout = 'ajax';

    $data = $this->HostingAccount->HostPackage->find('list',array('conditions' => array(
                    'HostPackage.host_id' => $this->data['HostingAccount']['host_id'])));

    //can't get access to $options in my View
    set('options',$data);

In the default.ctp I have:

    echo $this->Js->writeBuffer(array('inline' => true));  

In the ajax.ctp:

    <?php echo $this->fetch('content'); ?>

I've tried playing around with different settings like commenting out $this->autoRender and just trying to access $options from default view instead of ajax and it just doesn't let me get the data from it.

Please help, thank you!

Foi útil?

Solução

Ok so I was missing a View for the function which was handling the AJAX request, by simply creating a View ajax_populate.ctp for the function ajax_populate() in the controller I was able to retreive the data from that view and work with it further:

Controller:

        public function ajax_populate() {
        $this->layout = 'ajax';

        $data = $this->Domain->HostingAccount->HostPackage->find('list',array('conditions' => array(
                        'HostPackage.host_id' => $this->data['Domain']['host_id'])));

        $this->set('options', $data);
    }

View:

<?php 
echo "<div><select>";
foreach($options as $key => $val){
    echo "<option value='$key'>$val</option>";
}
echo "</select></div>";

Original View (from which the ajax is called):

       echo $this->Js->get('#DomainHostId')->event('change',$this->Js->request('ajax_populate',
                    array('method' => 'POST',
                          'async' => true,
                          'update' => '#host_packages',
                          'dataExpression' => true,
                          'data'=> $this->Js->serializeForm(array(
                             'isForm' => true,
                             'inline' => true
                            ))
                          //'evalScripts' => true
                         )));

?>

HTML: (div)

   <div id="host_packages"></div>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top