Code to be run twice in Ajax request

醉酒当歌 提交于 2019-12-25 16:44:00

问题


In the if(!$validate) block mail function truly runs once.

I'm wondering why In the if($validate) block mail function runs twice !!!???

The problem raise only in Ajax request and in first time that view page loads. after that the if($validate) block runs once, on the other requests if($validate) block truly runs once.

public function  actionCEmail()
{
    $model = $this->loadModel(Yii::app()->user->id);
    $model->scenario = 'CEmail';

    if(isset($_POST['User'])){
        $model->attributes = $_POST['User'];
        $validate=$model->validate();

        if(Yii::app()->request->isAjaxRequest){
            if(!$validate) {
                $to='test@example.ds';
                $subject='test';
                $message='this is test';
                mail($to,$subject,$message);
                Yii::app()->end();
            }
            if($validate){
                $to='test@example.ds';
                $subject='test';
                $message='this is test';
                mail($to,$subject,$message);
                Yii::app()->end();
            }
        }
    }

    if(Yii::app()->request->isAjaxRequest)
        $this->renderPartial('_cemail',array('model'=>$model),false,true);
    else
        $this->render('update',array('model'=>$model,'form'=>'_cemail'));
}

If you need any information tell me put it.


回答1:


Solved: Reason that the if($validate) block was performed twice was: once when Ajax validation and once again when clicking on ajaxSubmitButton for submitting the form.

Is there a way for distinguish these two from one another? order that to be understood what times clicked on ajaxSubmitButton? or other things?




回答2:


if(Yii::app()->request->isAjaxRequest)
{
$error=CActiveForm::validate(array($model,$profile));
if($error!='[]') {
$to='test@example.ds';
                $subject='test';
                $message='this is test';
                mail($to,$subject,$message);
Yii::app()->end();
        }}
if(isset($_POST['User'])){
        $model->attributes = $_POST['User'];
$validate=$model->validate();
if($validate){
                $to='test@example.ds';
                $subject='test';
                $message='this is test';
                mail($to,$subject,$message);
                Yii::app()->end();
            }}

Try to make something like this.



来源:https://stackoverflow.com/questions/16112197/code-to-be-run-twice-in-ajax-request

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!