Monday, September 29, 2008

Simple Ajax link to update div

Hi,
I have been struggling a bit to get a simple Ajax link working. I have
searched this group for help in older posts, but it seems like a key
tutorial (the grahambird one) is no longer available - perhaps because
it no longer works with 1.2.

If there is a tutorial somewhere that covers this, please let me know.
I think I have gone over all the available (through google) CakePHP
ajax tutorials, but unfortunately without success - some are outdated
(pre 1.2), others do not have full working code etc...

In any case, here is my code:
========================================
index.ctp
<h2>Ajax 3 - testing ajax link and button</h2>
<?php
$link_array1 = array('controller'=>'tests' ,
'action'=>'ajaxfcn1'); // the function to execute: function
ajaxfcn1 in controller tests

$link_array2 = array( 'update' => 'divout',
'complete' => 'alert( "Executed OK" )' ); // the div we want
to update

echo ($ajax->link('this an ajax link', $link_array1,
$link_array2,'Confirmation String'));

echo "<br/><br/>";

echo ($ajax->submit('this is an ajax button', $link_array1,
$link_array2,'Confirmation String'));
?>
<br/>
<div id="divout">
the div to update
</div>
========================================
tests_controller.php
<?php
class TestsController extends AppController {

var $name = 'Tests';
var $uses = array(); // prevent cake from looking for a database
model

function index() {

}

function ajaxfcn1() {
$this->render('index', 'ajax');
}
}
?>
========================================

So basically I have a view with an ajax link, an ajax button, and a
<div>. I would like to change the text in the div when I click the
link or button. At this point, nothing happens when I click them. I am
not sure if the ajaxfcn1() function is even executing at this point.
What's an easy statement to add in ajaxfcn1() to determine if the code
is executing? What function would I use to set the new div text from
within ajaxfcn1()?

Thanks

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: