Piece Of Code To Be Run Twice In Ajax Request

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.

Amazed!!!!!!!!!!

Last night i have slept after a very effort for solving the problem. Now that i wake up and test the script again, i’ve amazed, the [color="#0000FF"]if($validate)[/color] block works!

It has worked truly until i went to run the [color="#0000FF"]if(!$validate)[/color] block. After once the [color="#0000FF"]if(!$validate)[/color] block have ran, the [color="#0000FF"]if($validate)[/color] block is executed twice. !!!!!!!!!!!!!

Solved: Reason that the [color="#0000FF"]if($validate)[/color] 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?