Title: Yii Controller call from a View?
Tags: php;jquery;yii;yii-chtml
Question: I have a view that has a form. I have a button which calls a controller via Ajax.
echo CHtml75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0submitButton('Generate', array(
'ajax'        =&gt; array(
'type'    =&gt; 'POST',
'url'     =&gt; CController75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0reateUrl('Calculator/generateRetailers'),
'update'  =&gt; '#div_retailers'
)
));
The action controller what it does is to gather some data from MySQL and then renderPartial HTML tables into my form. Exactly in the div #div_retailers. This is the create option. Now I am trying to implement the update action which should render the information provided in the create action and draw the tables.
I would like to be able to call generateRetailers action controller from my view. Something like this:
<div id="div_retailers"&gt;
</div&gt;
<script type="text/javascript"&gt;
// I would like to call a url using jQuery?
$.ajax({
url: "/Calculator/generateRetailers"
});
</script&gt;
How can I achieve this?
Thanks
Comment: I don't really understand your question/problem -- you seem to be showing how to call a controller action from a view (via Ajax). Can you clarify?

Comment: @ldg Yes! exactly I want to call a controller via Ajax from within a view. But from raw JavaScript with no buttons or links or any HTML object. Is it possible?

Here is the accepted answer: Do the exact same thing you already do in the widget:
<script type="text/javascript"&gt;
// I would like to call a url using jQuery?
$.ajax({
url: "<?php echo CController75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0reateUrl('Calculator/generateRetailers');?&gt;"
</script&gt;
Comment for this answer: @ivan.freire: Depends on what function you are calling, how you are calling it, if it causes errors, etc. In any case, that sounds like a different question.
Comment for this answer: now before making the call I need to call another JavaScript function. If I call JavaScript before the $.ajax, then the controller is not called. How can I do this? Thanks
Comment for this answer: sorry definitely it is another question. Sorry, I had a JavaScript error, wrong index in an array. Anyway, your solution worked for me. Thanks.

Here is another answer: Interesting, there is no built-in url generator for ajax. So im calling ajax with helper variable, which is generated by my yii app.
I use Yii75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0pp()-&gt;clientScript("config.url = ".Yii75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0pp()-&gt;createUrl() . ");
and then in pure javascript i can use a global variable config where i have generated urls.
$.ajax({
url: config.url

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2011:00:09/19/11', 'FavoriteCount': 0.0, 'PostTypeId': 1, 'Tags': '<php><jquery><yii><yii-chtml>', 'Title': 'Yii Controller call from a View?', 'ViewCount': 7837, 'id': 7478357, 'user_AboutMe': '', 'user_AccountId': 440142.0, 'user_DisplayName': 'ivantxo', 'user_Reputation': 757.0, 'user_Views': 92.0, 'user_id': 829549.0}}