PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, March 6, 2022

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

 March 06, 2022     ajax, php, yii     No comments   

Issue

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

?>

Solution

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



Answered By - Mohammed Iqbal Khan
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing