问题
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