More information about this topic
Reset database
 | 
Example1: Ajax request using ajaxLink

protected/modules/AjaxModule/views/ajax/ajax_request.php


echo CHtml::ajaxLink(
	'Test request',          // the link body (it will NOT be HTML-encoded.)
	array('ajax/reqTest01'), // the URL for the AJAX request. If empty, it is assumed to be the current URL.
	array(
		'update'=>'#req_res'
	)
);
?>

<div id="req_res">...</div>

protected/modules/AjaxModule/controllers/AjaxController.php

	public function actionReqTest01() {
		echo date('H:i:s');
		Yii::app()->end();
	}
Example2: Ajax request using ajaxLink with loading image

protected/modules/AjaxModule/views/ajax/ajax_request.php


echo CHtml::ajaxLink(
	'Test request',          // the link body (it will NOT be HTML-encoded.)
	array('ajax/reqTest01Loading'), // the URL for the AJAX request. If empty, it is assumed to be the current URL.
	array(
		'update'=>'#req_res_loading',
        'beforeSend' => 'function() {           
           $("#maindiv").addClass("loading");
        }',
        'complete' => 'function() {
          $("#maindiv").removeClass("loading");
        }',        
	)
);
?>

<div id="req_res_loading">...</div>

protected/modules/AjaxModule/controllers/AjaxController.php

	public function actionReqTest01Loading() {
        sleep(4);   // Sleep for 4 seconds just to demonstrate the Loading Image can be seen, for learning purpose only		
        echo date('H:i:s');
		Yii::app()->end();
	}
Example3: Ajax request using ajaxButton
...

protected/modules/AjaxModule/views/ajax/ajax_request.php


<div class="form">
<?php echo CHtml::beginForm(); ?>

<div class="row">
<?php echo CHtml::label('Some text', 'some_text'); ?>
<?php echo CHtml::textField('some_text', date('H:i:s')); ?>
</div>

<?php
echo CHtml::ajaxSubmitButton(
	'Submit request',
	array('ajax/reqTest03'),
	array(
		'update'=>'#req_res02',
	)
);
?>

<?php echo CHtml::endForm(); ?>
</div><!-- form -->

<div id="req_res02">...</div>

protected/modules/AjaxModule/controllers/AjaxController.php

	public function actionReqTest03() {
		echo CHtml::encode(print_r($_POST, true));
	}
Fork me on GitHub