Generates a link that can initiate AJAX requests.
Syntax
public static string ajaxLink(string $text, mixed $url, array $ajaxOptions=array ( ), array $htmlOptions=array ( ))
Example
echo CHtml::ajaxLink( $text = 'Click me', $url = '/', $ajaxOptions=array ( 'type'=>'POST', 'dataType'=>'json', 'success'=>'function(html){ jQuery("#your_id").html(html); }' ), $htmlOptions=array () );
Output:
Click me
Return values
string, the generated link
Examples
Example #1 : Ajax request using ajaxLink
//In view: 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' )); echo '
...'; //In controllerpublic function actionReqTest01() { echo date('H:i:s'); Yii::app()->end();}Output:
Example #2 : Ajax request using ajaxLink with loading image
//In view: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"); }', )); echo '
...'; //In controller: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();}Output:
References
http://www.yiiframework.com/doc/api/1.1/CHtml#ajaxLink-detail
http://www.yiiplayground.com/index.php?r=AjaxModule/ajax/ajaxRequest#ajaxLink