4

I want to create the most basic ajax link there is in Cakephp 2.0.

In index.ctp i have

 <?php
echo $this->Js->link('myLink', array('controller'=>'technologies', 'action'=>'view'), array('update'=>'#success'));
?>
 <div id="success"></div>

in TechnologiesController.php i have

public function view(){
    $this->set('msg', 'message');
    $this->render('view', 'ajax'); 
}

and in view.ctp i have

<?php echo $msg;?>

Instead of setting the view in the success div, it navigates to the http://local.cake.com/technologies/view page to display the message.

Any help much appreciated!

tereško
  • 58,060
  • 25
  • 98
  • 150
glasspill
  • 1,290
  • 4
  • 21
  • 36

6 Answers6

6

By default scripts are cached, and you must explicitly print out the cache. To do this at the end of each page, include this line just before the ending tag:

echo $this->Js->writeBuffer(); // Write cached scripts

I am using this at the end of my default.ctp in the Layouts Folder

Markus
  • 76
  • 1
  • 2
1

So, in total the code would look like this - and it works for me (CakePHP 2.2.4):

index.ctp:

<?php
echo $this->Js->link('myLink', array('controller'=>'technologies', 'action'=>'view'), array('update'=>'#success'));
?>

<div id="success"></div>

echo $this->Js->writeBuffer();

Thank you a lot, this helped me understand how things work in 2.0 and above :)

1

Be sure to set $components = array('RequestHandler') in your controller

Wil
  • 540
  • 1
  • 4
  • 9
0

I think you might have to put this:

echo $this->Js->writeBuffer();

somewhere - for example right below the $this->Js->link call.

Hope that helps!

DaveShaw
  • 52,123
  • 16
  • 112
  • 141
Chris
  • 1
0

I just put this:

echo $this->Js->writeBuffer();

it work for me, hope i will for you

Rohit Dubey
  • 1,234
  • 15
  • 15
0

try $this->autoRender = FALSE;

Saanch
  • 1,814
  • 1
  • 24
  • 38