Update content using AJAX and partialRender
The simplest way to update content using AJAX is to use the partialRender method.
For this example, I have three files: one controller (HelloWorldController. php) and two views (index. php and _ ajaxContent. php)
Controllers/HelloWorldController. php
class HelloWorldController 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); } }
MyValue is the "loaded content" set by actionIndex. this variable is passed to index. php and _ ajaxContent. php when the view is loaded"
Note: If your controller file uses accessRules, you need to modify accessRules () and add the appropriate method name-in this case, updateajax is like this:
array('allow', // allow all users to perform 'index' and 'view' actions 'actions'=>array('index','view','updateajax'), 'users'=>array('*'), ),Views/helloWorld/index. php
renderPartial('_ajaxContent', array('myValue'=>$myValue)); ?>
'#data')); ?>
AjaxButton calls "actionUpdateAjax" and inserts the returned data into this "data" div
Views/helloWorld/_ ajaxContent. php
Show $ myValue
Now, we run index. php? R = helloWorld
No data is found in one comment.