Question

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

Was it helpful?

Solution

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.

OTHER TIPS

try this

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

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);
       }
   }',
));
?>

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

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(

This is simple example of Yii Ajax Submit button:

<?php
echo CHtml::ajaxSubmitButton(
'Submit',
'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);
       }
   }',
)
);
?>

More detail at http://www.codexamples.com/90/chtml-ajaxsubmitbutton/

Ajax submit button in view:

echo CHtml::ajaxSubmitButton('Save', array('/controller/action/id/' .     $id), array(
                'type' => 'post',
                'dataType' => 'json',
                'update' => '#mydiv',
                'beforeSend' => 'function(){}',
                'complete' => 'function(){}',
                'success' => 'js:function(data){
                    if(data.status=="1"){
                        html = "<div class=\"alert in alert-block fade alert-success\"><a class=\"close\" data-dismiss=\"alert\">x</a>"+data.msg+"</div>";
                        $("#status-msg").html(html);

                        }else{
                        html = "<div class=\"alert in alert-block fade alert-error\"><a class=\"close\" data-dismiss=\"alert\">x</a>"+data.msg+"</div>";
                        $("#status-msg").html(html);
                        }
                    }',
                    ), array('class' => "btn btn-primary")
            );

In Controller action:

        $return = array();
        $return['status'] = '1';
        $return['msg'] = 'Record Updated Successfully';

        echo CJSON::encode($return);
        Yii::app()->end();

Change return array as per requirement.

You have to set the fourth parameter of renderPartial function to true, so that Yii will include the required JS files for the proper working of the view.

$this->renderPartial('_form',array('model'=>$model), false, true);

In the controller function you need to send 3 more parameters (total 4 parameters)

your function will be like this

public function actionLogin()
{
  this->renderPartial('//blocks/user_info','',false,true);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top