Update content in AJAX with partialRender

You are viewing revision #1 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.

next (#2) »

The most easy way to update content in AJAX is to use the partialRender method.

For this exemple I have three files: a controller (HelloWorldController.php) and two views (index.php and _ajaxContent.php)


controllers/HelloWorldController.php
class HelloController extends CController
{
	public function actionIndex()
	{
		$data = array();
		$data["myValue"] = "Content loaded";
		
		$this->render('index', $data);
	}
	
	public function actionUpdateAjax()
	{
		$data = array();
		$data["myValue"] = "Content updated in AJAX";
		
		$this->renderPartial('_ajaxContent', $data, false, true);
	}
}

The actionIndex set myValue to "Content loaded" and this variable is passed to the view "index.php" and to "_ajaxContent.php"


views/hello/index.php
<div id="data">
   <?php $this->renderPartial('_ajaxContent', array('myValue'=>$myValue)); ?>
</div>

<?php echo CHtml::ajaxButton ("Update data",
                              CController::createUrl('hello/UpdateAjax'), 
                              array('update' => '#data'));
?>

The ajaxButton call "actionUpdateAjax" and the returned data are inserted in the div "data"


views/hello/_ajaxContent.php
<?php echo $myValue ?>

Display $myValue


Now, run index.php?r=hello

Enjoy!

43 0
46 followers
Viewed: 351 530 times
Version: Unknown (update)
Category: Tutorials
Tags: AJAX
Written by: Burzum
Last updated by: hairylunch
Created on: Sep 30, 2009
Last updated: 11 years ago
Update Article

Revisions

View all history

Related Articles