简体   繁体   中英

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.
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

<?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. 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'=>'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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM