Yii Controller call from a View?

ivantxo picture ivantxo · Sep 20, 2011 · Viewed 7.8k times · Source

I have a view that has a form. I have a button which calls a controller via Ajax.

    echo CHtml::submitButton('Generate', array(
        'ajax'        => array(
            'type'    => 'POST',
            'url'     => CController::createUrl('Calculator/generateRetailers'),
            'update'  => '#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">
</div>

<script type="text/javascript">
    // I would like to call a url using jQuery?
    $.ajax({
        url: "/Calculator/generateRetailers"
    });
</script>

How can I achieve this?

Thanks

Answer

Jon picture Jon · Sep 20, 2011

Do the exact same thing you already do in the widget:

<script type="text/javascript">
  // I would like to call a url using jQuery?
  $.ajax({
    url: "<?php echo CController::createUrl('Calculator/generateRetailers');?>"
  });
</script>