[英]Data is saved Twice to the database when ajax is used in yii
I used an ajax to save the form.But when I Save the data ,datas are saved mutilple times to the database. 我使用了ajax来保存表单。但是,当我保存数据时,数据会被多次保存到数据库中。
I am sharing my controller and form here please help me guys. 我在这里分享我的控制器和表格,请帮助我。
Controller 控制者
public function actionCreate()
{
$model=new Comments;
// Uncomment the following line if AJAX validation is needed
$this->performAjaxValidation($model);
if(isset($_POST['Comments']))
{
$model->attributes=$_POST['Comments'];
// echo '<pre>';print_r($model->attributes);die();
$valid = $model->validate();
if($valid){
if($model->save()){
echo CJSON::encode(array(
'status'=>'success'
));
}
}else{
$error =CActiveForm::validate($model);
if($error != '[]')
echo $error;
Yii::app()->end();
}
}
// $this->render('create',array(
// 'model'=>$model,
// ));
}
_form I here only giving the ajax method for saving _form我在这里只给出了保存的ajax方法
<?php
echo CHtml::ajaxSubmitButton('Save',CHtml::normalizeUrl(array('Comments/create','render'=>true)),
array(
'dataType'=>'json',
'type'=>'post',
'success'=>'function(data){
$("#Comments_email").val("");
$("#AjaxLoader").hide();
if(data.status == "success"){
$("#formResult").html("Comment Submitted");
$("#formResult").css({"color":"red"})
$("#comments-form")[0].reset();
}else{
$.each(data, function(key, val){
$("#comments-form #"+key+"_em_").text(val);
$("#comments-form #"+key+"_em_").show();
});
}
}',
'beforeSend'=>'function(){
$("#AjaxLoader").show();
}'
),array('id'=>'submit','class'=>'btn btn-success'));
?>
Hai friends I Found out the solution for this problem. 海友我找到了解决这个问题的办法。
I am sharing the answer here for further refernce. 我在这里分享答案以供进一步参考。
This issue appear because each time you display a view with an ajaxSubmitbutton, an event handler is created. 出现此问题的原因是,每次您显示带有ajaxSubmitbutton的视图时,都会创建一个事件处理程序。 so, a way to solve that is to destroy the handler just after using it.
因此,一种解决方法是在使用处理程序后立即销毁该处理程序。
_Form _形成
Please add a undelegate() in beforeSend() like this 请像这样在beforeSend()中添加undelegate()
'beforeSend'=>'function(){
$(\'body\').undelegate(\'#submit\', \'click\');
$("#AjaxLoader").show();
}'
The the full code will be like this: 完整的代码将如下所示:
<?php
echo CHtml::ajaxSubmitButton('Save',CHtml::normalizeUrl(array('Comments/create','render'=>true)),
array(
'dataType'=>'json',
'type'=>'post',
'success'=>'function(data){
$("#Comments_email").val("");
$("#AjaxLoader").hide();
if(data.status == "success"){
$("#formResult").html("Comment Submitted");
$("#formResult").css({"color":"red"})
$("#ajax-comments-form")[0].reset();
}else{
$.each(data, function(key, val){
$("#ajax-comments-form #"+key+"_em_").text(val);
$("#ajax-comments-form #"+key+"_em_").show();
});
}
}',
'beforeSend'=>'function(){
$(\'body\').undelegate(\'#submit\', \'click\');
$("#AjaxLoader").show();
}'
),array('id'=>'submit','class'=>'btn btn-success'));
Thank you for all your supports 感谢您的支持
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.