Fire off alerts on button click

16,887

Solution 1

Not sure if this is your problem, but renderPartial() does not play well with AJAX. It is a known Yii issue:

http://www.yiiframework.com/forum/index.php/topic/24699-yii-20-ajaxrenderpartial-conflict/ http://www.yiiframework.com/forum/index.php?/topic/10427-ajax-clientscript

I ran into the problem when I tried to load a form via AJAX, then submit it back using CHtml::ajaxSubmitButton.

Solution 2

try this

this->renderPartial('//blocks/user_info','', false, true);

Solution 3

in my code it works try this

<?php
echo CHtml::ajaxSubmitButton('Save','your-url',array(
   'type'=>'POST',
   'dataType'=>'json',
   'success'=>'js:function(data){
       if(data.result==="success"){
          // do something on success, like redirect
       }else{
         $("#some-container").html(data.msg);
       }
   }',
));
?>

Solution 4

Does beforeSend work? What if you try "success" instead of "complete"?

Solution 5

I have this problem(bug?) too. Try change 'beforeSend' and other callbacks in this view:

'beforeSend':'js:alert("complete");'

without "function(){}" and with "js".

P.S. Sorry, my English is bad(

Share:
16,887
Rafael Sedrakyan
Author by

Rafael Sedrakyan

Updated on June 16, 2022

Comments

  • Rafael Sedrakyan
    Rafael Sedrakyan almost 2 years

    When I click the button, alerts aren't performed. What am I doing wrong? Maybe I need to include some js files in my header section?

    Here is my login form view:

    <?php echo CHtml::beginForm(); ?>
    
    <div class="row">
    <?php echo CHtml::label('username', 'username'); ?>
    <?php echo CHtml::textField('username'); ?>
    </div>
    <div class="row">
    <?php echo CHtml::label('password', 'password'); ?>
    <?php echo CHtml::textField('password'); ?>
    </div>
    <?php
    echo CHtml::ajaxButton('sign in', array('site/login'),array(
            'type'=>'POST',
            'update'=>'#mydiv',
            'beforeSend' => 'function(){
                alert("beforeSend");
            }',
            'complete' => 'function(){
                alert("complete");
                }',
    
    ));
    ?>
    <?php echo CHtml::endForm(); ?>
    <div id="mydiv" style="color:white;">...</div>
    

    Here is my code in the controller:

    public function actionLogin()
    {
        $this->renderPartial('//blocks/user_info');
    }
    

    user_info just echoes some text