Fire off alerts on button click

Rafael Sedrakyan picture Rafael Sedrakyan · Feb 8, 2012 · Viewed 16.9k times · Source

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

Answer

siliconrockstar picture siliconrockstar · Jul 3, 2012

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.