Social Icons

Pages

Monday, May 13, 2013

Yii ajax Link

This is just a simple tutorial for creating an ajaxLink in Yii.


In view that contains the link:
echo CHtml::ajaxLink(
  "Link Text",
  Yii::app()->createUrl( 'myController/ajaxRequest' ),
  array( // ajaxOptions
    'type' =>; 'POST',
    'beforeSend' => "function( request )
                     {
                       // Set up any pre-sending stuff like initializing progress indicators
                     }",
    'success' => "function( data )
                  {
                    // handle return data
                    alert( data );
                  }",
    'data' => array( 'val1' => '1', 'val2' => '2' )
  ),
  array( //htmlOptions
    'href' => Yii::app()->createUrl( 'myController/ajaxRequest' ),
    'class' => $class
  )
);

In the MyController.php file:
public function accessRules()
{
  return array(
    array('allow',
          'actions'=>array('ajaxrequest'),
          'users'=>array('@'),
    ),
    // ...
    array('deny',  // deny all users
          'users'=>array('*'),
    ),
  );
}


public function actionAjaxRequest()
{
  
  echo "some sort of response";
 
  Yii::app()->end();
}





No comments:

Post a Comment