0

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.

msoa
  • 1,339
  • 3
  • 14
  • 33
  • Are you sure it runs once in one block but twice in the other? Both blocks are doing the identical same thing. Why not change both to say something specific, e.g. `$message = 'this is a test from !$validate'` so you can identify which block the mail was sent from? – Marc B Apr 19 '13 at 19:51
  • **@MarcB:** I'm sure, also try your way, but result was same. – msoa Apr 20 '13 at 02:40
  • **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 `if($validate)` block works! It has worked truly until i went to run the `if(!$validate)` block. After once the `if(!$validate)` block have ran, the `if($validate)` block is executed twice. !!!!!!!!!!!!! – msoa Apr 20 '13 at 05:08

2 Answers2

0

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?

msoa
  • 1,339
  • 3
  • 14
  • 33
0
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.

ineersa
  • 3,445
  • 30
  • 40