Yii:成功表单提交的JavaScript回调

I want a JavaScript callback on successful save of an active form. Where can I put the JavaScript callback?

Do I need to make the form AJAX in order to do this or is is doable with regular (non-AJAX) forms?

In fact, I am going to put this form inside an iframe (inside a jQuery UI modal dialog).

When the form in the iframe is saved I need to do two things:

  1. close the dialog;
  2. update the list of things in a <select> of the main HTML document.

Try to put ajax handler on form submit, here is a live code from my project as example:

 echo CHtml::ajaxSubmitButton('submit',          
        array('index'), 
        array(
                'type'=>'POST',
                'data' => array(),
                'success' => 'js:function(){/*callback*/}', 
        ));

Please, remember to give this button an unique (prefix_random) id to avoid inconvenionces

You can add button to your form:

<?=CHtml::ajaxSubmitButton('Save', Yii::app()->createUrl('some_url'),
            array('success' => 'function(response){afterSubmitForm(response);}'),
            array('class' => 'btn btn-primary'));
        ?>

Where afterSubmitForm() js function, which get server response as parameter.