Friday, March 2, 2012

Make ajax request in yii framework

To make ajax request in yii framework is simple to use. For the usual  ajax request from jquery we can use this pattern like this..

$.ajax({
    url:"http://localhost/request.php",//this is the request page of ajax
    data:{params1:param1,param2:param2},//data for throwing the expected url
    type:"POST",//you can also use GET method
    dataType:"html",// you can also specify for the result for json or xml
    success:function(response){
         $('#result_selector').html(response);
    },
    error:function(){
         alert('Failed request data from ajax page");
    }

But in yii the ajax function has been integrated to the yii native function. There are many choices for you to make ajax call through button, link or other components.. From the documentation we can take a look of this part.

ajax()Generates the JavaScript that initiates an AJAX request.CHtml
ajaxButton()Generates a push button that can initiate AJAX requests.CHtml
ajaxSubmitButton()Generates a push button that can submit the current form in POST method.CHtml
That's the list of function supported by yii function. you can use that to populate the ajax request just use that function.

Oke let's take a look below code for the example

<?php
       echo CHtml::ajaxButton(
            'Submit request',
            array('test/function'),
            array('data'=>array('test'=>$model->id)),
            array('update'=>'#update_selector')//this is the update selector of yours $('#update_selector').load(url);
        );
?>
And then don't forget if you want to make an ajax call you should have to use renderPatial so the whole contents is not included.
public function actionFunction() {
 if(Yii::app()->request->isAjaxRequest){
  $this->renderPartial("_form",array('model'=>$model),false,true);
  Yii::app()->end();

 }
}
That's the easy look of yii function..


7 comments:

  1. Handita,
    I have read a few of your articles and it is great to see someone creating quality content on Yii. Wish there were more developers that were passionate about the framework. We have been working on an open source CRM application that is written in PHP utilizing JQuery, Yii, and RedBeanPHP and relies heavily on test driven development.

    It might be one of the most complex projects on Yii to date. Right now, we have 1000+ unit tests running across eight server configurations. We utilize selenium as well for a nice set of functional tests too. It would be incredibly helpful to get your technical feedback and recommendations so that we can improve the application. Take a look and let me know what you think:
    http://zurmo.org

    ReplyDelete
  2. Error: Undefined variable. Model... Please at least trigger your code before posting.

    Thanks

    ReplyDelete
  3. @ravi you should have to know the basic of using yiiframework too.

    ReplyDelete
  4. Hi, thank you very much for help. I am going to test that in the near future. Cheers

    yii framework development company india

    ReplyDelete
  5. This post is very informative for everybody. I would like to appreciate your work...................Please contact us for Oracle Fusion HCM training details in our Erptree Training Institute

    ReplyDelete
  6. This is just the kind of information that I had been looking for a long time. Thank you very much...........................Click Here to find out more regarding Oracle Financials Training.

    ReplyDelete
  7. This is really too useful and has more ideas from your blog. Keep sharing more blog like this, thank you. We are waiting for your new blog and for useful information.
    Click Here For More Information.....
    Oracle RAC Training in Al Karama, Dubai

    ReplyDelete