Ajax Form Submitting Twice

Hi Friends,

I have a problem when trying to use example from below link:

http://www.yiiframework.com/forum/index.php/topic/37075-form-validation-with-ajaxsubmitbutton/

I used simple approach for ajax validation before submitting, but seems it submit twice to server, here my code:

View:




<?php

    $form=$this->beginWidget('CActiveForm', array(

        'enableAjaxValidation'=>true,

        'id'=>'test-form',

        'enableClientValidation'=>true,

        'action'=>Yii::app()->createUrl('Site/Test'),

        'clientOptions'=>array(

            'validateOnSubmit'=>true,

            'validateOnChange'=>false,

            'afterValidate'=>'js:function(form,data,hasError){

                        alert(hasError);

                        if(!hasError){

                        alert("gamasuk");

                                $.ajax({

                                        "type":"POST",

                                        "url":"'.Yii::app()->createUrl('Site/Test').'",

                                        "data":form.serialize(),

                                        "success":function(data){alert("banana");},


                                        });

                                }

                        }'

        ),

    ));


    ?>


    <?php $model1 = new SignUpViewModel(); ?>


    <div class="login-form">

        <?php echo $form->labelEx($model1,'username'); ?>

        <?php echo $form->textField($model1,'username',array('size'=>60,'maxlength'=>500)); ?>

        <?php echo $form->error($model1,'username'); ?>


        <br/>

        <?php echo $form->labelEx($model1,'password'); ?>

        <?php echo $form->textField($model1,'password',array('size'=>60,'maxlength'=>500)); ?>

        <?php echo $form->error($model1,'password'); ?>


        <br/>

        <input id="asd" type="submit" value="ddd" onclick="javascript:return false;"/>

        <?php echo CHtml::submitButton('Save'); ?>





    </div>

    <?php $this->endWidget(); ?>



Controller:




public function actionTest(){

        $model=new SignUpViewModel();

        $this->performAjaxValidation($model);


        if(isset($_POST['SignUpViewModel']))

        {

            $model->attributes=$_POST['SignUpViewModel'];

            $valid=$model->validate();

            if($valid){


                //do anything here

                echo CJSON::encode(array(

                    'status'=>'success'

                ));

                Yii::app()->end();

            }

            else{

                $error = CActiveForm::validate($model);

                if($error!='[]')

                    echo $error;

                Yii::app()->end();

            }

        }

    }



The code is working, but i got twice request to server,

i put ‘action’=>Yii::app()->createUrl(‘Site/Test’) because my form is in dialog box, so if didn’t put the ‘action’ here, it always go to parent url instead of site/test.

anyone can help me?

THanks a lot

Andi

Anyone can help :(?

Hi,

Please see this

http://www.yiiframework.com/forum/index.php/topic/43977-ajax-client-side-validation-without-refreshing-page/page__p__208536__fromsearch__1#entry208536

Hi Ankit,

I didn’t realized that you already write the answers in my previous thread. it works! :)

Thanks a lot