Simple Yii ajaxLink

In: Programming|Yii

12 May 2010

This is just a simple tutorial for creating an ajaxLink in Yii.  Since I looked all around and there wasn’t anything this straightforward, I thought I’d write it up myself. Feel free to add any comments or suggestions.  My goal is to create a link that passes data through POST to one of my controller actions using the Yii AJAX interface.  While researching how to do this the “Yii” way, I found no less than 5 different examples, none of which worked, and none of which purported to do exactly what I was looking to do.

If you’re just looking for the simple example, here it is.

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
  )
);

Make sure to allow the user access to the particular action you’re calling through your AJAX request.

In the MyController.php file:

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

Now let’s handle that request shall we?

Further along in MyController.php:

public function actionAjaxRequest()
{
  $val1 = $_POST['val1'];
  $val2 = $_POST['val2'];
 
  //
  // Perform processing
  //
 
  //
  // echo the AJAX response
  //
  echo "some sort of response";
 
  Yii::app()->end();
}

No related posts.

  • reed1

    just what I need, thanks

  • andrea

    great, thank you for the example!

  • http://b3dmultitech.com Paul

    Awesome – thanks – i tried it and it works! Can u explain what
    ‘users’=>array(‘@’),

    and

    ‘users’=>array(‘*’),

    is for?

    Also, do you have any code for the spinners?

  • Bryan

    ‘users’=>array(‘@’) means that all authenticated users can access the controller’s methods. ‘users’=>array(‘*’) selects all users, but since it is under the ‘deny’ portion of the access rules it means that if no other rules have been matched, then deny access.

    For the spinners, search around for “progress indicators” or “spinner gif” online and you’ll find plenty of examples of the actual spinner images. As for displaying them, it’s a matter of placing some javascript in the beforeSend function that either changes a css class or places the spinner image in the document.

  • http://talkweb.eu Bogomil Shopov

    Thank you very much. :)

    //Bogo

  • http://b3dmultitech.com Fire

    can you post an example with spinners? Thanks!!!

  • Plamtod

    What is meaning of “htmlOptions”. In first part of function url is yet set by createUrl and what is var $class. 10x

  • outlier

    nice example ! I’m reading this book : http://book.vnexpert.org/2011/05/agile-web-application-development-with.html to use Yii Frame

  • http://www.phillipsdesign.com.au Andrew Phillips

    Thanks for the post, I’m building my first Yii app and this has been much more helpful than the included documentation. Just a correction – there’s a stray semi-colon inside the ajaxOptions array that was giving me an Internal Server Error.

  • http://codeperl.wordpress.com/2011/10/15/yii-ajax-link-configurations/ Yii Ajax Link configurations. « Codeperl's Knowledge Sharing System

    [...] Ajax Link configurations.http://lostmahbles.com/simple-yii-ajaxlink/Like this:LikeBe the first to like this post. [...]

  • Timofey

    ‘type’ =>; ‘POST’,”;” is not needed i think

  • F Ghafoor54

    where is the book dude

About LostMahbles

Bryan's Bio Picture

My name's Bryan Marble. I'm the founder of 5or7 Software, a bootstrapped software company in beautiful Manchester, NH.


I'm a former developer at HubSpot and I love talking startups, web usability and web apps in general and I'm always looking for new people to geek out with.

I'm currently developing GroupTrip, a group travel planning site that helps friends coordinate travel research and track expenses so that they can focus on the experience of the trip and not the logistics of traveling in groups. Check out the site, sign up for the e-mail list, and let me know if you have any feedback!

Photostream

    Crisp Morning
    Cote-Breton
    Cote-Breton
    Cote-Breton
    Cote-Breton
    Cote-Breton