How to implement AJAX that would get data from models?

Hi,

I’ve an application where all the initial contents have been displayed on a view.

Now on clicking some buttons i want to call some Ajax function which i stored them on models and then i want to update the current view with the returned content!!

Here I don’t want to update the whole view but just a portion of the current view with the information returned…

And I’ve another doubt, how can i pass the values of a certain model or a session value to the Ajax calls , just now i used to define the js functions in the main.php of layouts folder.

for example


var role = 1; //  consider if i need this value to be fetched from any session or model, what can i do?

function regInvite(role){

 if(role==1 || role==3){

    //some code

 }

 else if(role==2){

   //some code

 }

 var receiverUserIds = FB.ui({ 

  method : 'apprequests',

 },

 function(receiverUserIds) {

   if(receiverUserIds){  

       	$.ajax({

        	type: "POST",

        	url: "file.php",

                message: msg // this message will be decided on the first if block where role is checked

                data: "r=controller/action&req_ids="+receiverUserIds.request_ids,

		success: function(msg){        // consider if msg is html content which need to updated back to view, what can i do?

		if(msg =="1"){

		  window.location.reload();

		}

		}

	});

    }

  });

}

Take a look at this wiki.

Thanks.

But consider that If i dont want to add a ajaxbutton , ajaxlink and if i want to call ajax on some events like mouseover, click of an tab(ui tab) and the result will not be updated into a single div, the result will have two parts and will be updated in two different divs of a view??? How can i do this?

And please understand that I’m very new to YII and dont know the basic implementation of jquery into yii.

Thanks for helping.

Yii is a php framework, the js programming is up to you.

What yii gives you is an halper function, CHtml::ajax wich create an ajax call.

If you want to put some ajax on the onMouseOver, just do:


<div onMouseOver="<?php echo CHtml::ajax(...); ?>"

That’s it. If you need to update some div, just write your own js function.

In the wiki there is an example of action that answer to such ajax calls, is an example about how to return a json with a correctly encoded partial view.