Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fire off alerts on button click

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

like image 412
Rafael Sedrakyan Avatar asked Feb 08 '12 13:02

Rafael Sedrakyan


People also ask

How do I automatically close an alert after some time?

Closing Alerts To close the alert message, add a . alert-dismissible class to the alert container. Then add class="close" and data-dismiss="alert" to a link or a button element (when you click on this the alert box will disappear).

How can I stop a screen refresh after clicking OK on alert box?

Add break; at the end of an event and modify your ifthenelse block to reload window when true.

How do I get rid of OK alert?

showCancelButton and showConfirmButton are no longer needed. Instead, you can set buttons: true to show both buttons, or buttons: false to hide all buttons. By default, only the confirm button is shown.

How do you pop up an alert box when the browsers refresh button is clicked?

How do you pop up an alert box when the browsers refresh button is clicked? You can do it like this: window. onbeforeunload = function() { return “Data will be lost if you leave the page, are you sure?”; };


3 Answers

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.

like image 127
siliconrockstar Avatar answered Oct 04 '22 18:10

siliconrockstar


try this

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

like image 22
22francis Avatar answered Oct 04 '22 19:10

22francis


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);
       }
   }',
));
?>
like image 23
Ratnesh Avatar answered Oct 04 '22 18:10

Ratnesh