YII tutorial-update content using AJAX and partialRender

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.