当在yii中使用ajax时,数据被保存两次到数据库


Data is saved Twice to the database when ajax is used in yii

我使用ajax来保存表单。但是当我保存数据时,数据被多次保存到数据库中。
我在这里分享我的控制器和表单,请帮助我的家伙。

控制器

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 这里我只给出了保存

的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'));
?>

朋友们,我找到解决这个问题的方法了。
我把答案分享给大家,供大家参考。

出现这个问题是因为每次你显示一个带有ajaxSubmitbutton的视图时,都会创建一个事件处理程序。因此,解决这个问题的一种方法是在使用处理程序之后销毁它。

_Form
请像这样在beforeend()中添加一个取消委派()

'beforeSend'=>'function(){
                    $(''body'').undelegate(''#submit'', ''click'');
                    $("#AjaxLoader").show();
                }'  

完整的代码是这样的:

<?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'));  

感谢大家的支持