请求中运行两次的代码

请求中运行两次的代码

本文介绍了在 Ajax 请求中运行两次的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

if(!$validate)块中mail函数真正运行一次.

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

我想知道为什么在 if($validate) 块中 mail 函数运行两次!!!???

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

问题只出现在 Ajax 请求和第一次 view page 加载.之后 if($validate) 块运行一次,在其他请求上 if($validate) 块真正运行一次.

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='[email protected]';
                $subject='test';
                $message='this is test';
                mail($to,$subject,$message);
                Yii::app()->end();
            }
            if($validate){
                $to='[email protected]';
                $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.

推荐答案

已解决: if($validate) 块执行两次的原因是:一次在Ajax验证时再次在点击ajaxSubmitButton提交表单时再次.

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.

有没有办法区分这两者?为了理解点击 ajaxSubmitButton 的时间?还是其他?

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

这篇关于在 Ajax 请求中运行两次的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 22:53