Working With Ajax

hello, i’m new come to yii framework and have some problem.

i have register form with ajax valiation like this option in activeform


'enableAjaxValidation' => true,

    'clientOptions' => array(

        'validateOnSubmit' => true,

        //'validateOnChange' => true,

    ),

validation work but when i submit form don’t work.

i want to save information in database and show message to user your info saved and hide form otherwise show error message, i wrote this with json datatype .

this is my ajax button :


echo CHtml::ajaxSubmitButton('عضویت','',array(

        'success' => 

            'js: function(result) {

                if(result != "") {

                    //if (result.indexOf("{") != 0) {

                        if (result.error == false)

                        {

                            $("#divmsg").removeClass().addClass("alert alert-success");

                            $("#register").slideUp(700);

                        }

                        else

                        {

                            $("#divmsg").removeClass().addClass("alert alert-error");

                        }

                        $("#divmsg").html(result.msg);

                    //}

                }

            }',

        'error' =>

            'js: function() {

                $("#divmsg").removeClass().addClass("alert alert-error");

                $("#divmsg").html("خطا در فرستادن اطلاعات");

            }',

        'beforeSend' =>

            'js: function(){

                $("#loading").addClass("loading");}',

        'complete' =>

            'js: function(){

                $("#loading").removeClass("loading");}',

        'dataType' => 'json',

    ),

            array('class' => 'btn btn-primary'));

and this is my php code to response action


public function actionRegister() {

        $model = new User('register');

        if (isset($_POST['ajax']) && $_POST['ajax'] === 'register') {

            echo CActiveForm::validate($model);

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

        }

        if (isset($_POST['User'])) {

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


            if (Yii::app()->request->isAjaxRequest) {

                header('Content-Type: application/json; charset="UTF-8"');

                sleep(2);

                if ($model->validate()) {

                    //here save to database

                    echo json_encode(array(

                        'msg' => 'Congratulations!',

                        'error' => false

                    ));

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

                }

                else

                {

                    echo json_encode(array(

                            'msg' => $model->getErrors(),

                            'error' => true

                        ));

                     

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

                }

            }

            //else

                //$this->redirect(app()->homeUrl);

        

        }

        $this->render('register', array('model' => $model));

    }

what is the problem in my code ?

Hi,

Its working fine. your code is fine but your logic have problem… you are validating only if its ajax request…so when u submit it wont validate your code… Below code did that…

I hope this will help you…

[color=#000088]if[/color][color=#000000] [/color]color=#666600[/color][color=#000000] [/color][color=#666600]{[/color]

also i submit form with ajax, without refresh page …

Setting a break-point in the actionRegister handler and single stepping the code will make it easier to see what needs to be done.

If you do not have Xdebug or Zend Debugger set up in your development environment, I highly recommend that you do. Problems like there are easily fixed when you see what is going on.

[color="#006400"]NOTE: moved to proper section (General Discussion for Yii 1.1.x instead of Yii-powered applications)[/color]