user765368
user765368

Reputation: 20346

call cakePHP element via Ajax

just a quick question, Is it possible to call a cakePHP element via jQuery Ajax? I know the standard way to call an element in cakePHP is:

<?php echo $this->element('path_to_element', 'data_to_send_to_element'); ?>

But what if I wanna call my element inside the $.ajax or .load() function? How do I achieve this?

Thank you

Upvotes: 4

Views: 3785

Answers (1)

Joni
Joni

Reputation: 111219

To call anything in Cake, by Ajax or otherwise, you need to define an action in a controller. You could create a view too, but you can also have the action render an element directly by setting the viewPath. Example:

class MyController extends AppController {
    // Apply Ajax layout automatically
    var $components = array('RequestHandler');

    function doSomething() {
        $this->autoRender = false;
        ... // set parameters needed by the element...

        // render an element
        $this->viewPath = 'elements';
        $this->render('path_to_element');
    }
}

Upvotes: 7

Related Questions