How To Call Ajax

Sir, i tried like this also, but action is not working. when i remove htmlOptions in password file then action is working, but the page is displaying on next page.

Hi

can u send me your code please ?

hi

u can try the code in ur password page


<?php

$form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(

        

        'id' => 'changePass-form',

	'action'=>'password'

        'type' => 'horizontal',

        'enableAjaxValidation' => false,

        'clientOptions' => array(

                'validateOnSubmit' => true,

		'afterValidate' => 'js:checkErrors'


            ),

        'htmlOptions' => array(

                'onsubmit' => "return false;",

            ),




    ));

    ?>




<script>

                function checkErrors(form, data, hasError) {

                    var data=$("#changePass-form").serialize();

                    if(hasError!=true){

                        $.ajax({

                            type: 'POST',

                            url: '<?php echo Yii::app()->createAbsoluteUrl("user/password?id="); ?>',

                            data:data,

                            asyc:false,

                            success:function(data){

                               alert(data);

                               


                            }


                        });

                    }

                }

</scritp>



Now it’s working. thank you…

ok good try…

what’s problem in my code?

plese send the code

please uncomment the code in your rules and try the page


array('pass2', 'compare', 'compareAttribute' => 'pass1', 'on' => 'resetPass, changePassword'),



and comment the code


// array('pass1, pass2', 'application.components.validate.EPasswordStrength', 'on' => 'resetPass, changePassword'),

if below is not working

so u can change the action code in below.


 public function actionPassword($id) {


        $user = app()->user->getUser();

        if (isset($user->id) && $user->id === $id || app()->user->isAdmin()) {

            $model = $this->loadModel($id);

            $model->setScenario('changePassword');

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

			if((count(CJSON::decode(CActiveForm::validate($model)))>0)) {

				$this->render('/user/password',array(

						'model'=>$mode,

				));

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

			}

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

                if ($model->validate()) {

                    $model->password = crypt($_POST['User']['pass1'], Randomness::blowfishSalt());

                    if ($model->save()) {

                        app()->user->setFlash('success', 'Saved new password!');

                        $this->redirect(array('user/setting'));

                        //$this->redirect(array('update', 'id' => $model->id));

                        //$this->redirect('view');

                    }

                }

            }


            //$this->render('/user/password', array('model' => $model));

//            $this->renderPartial('/user/password', array('model' => $model),false,true);

        } else {

            // access denied for this user

            throw new CHttpException(403, 'Access Denied.');

        }

         $this->renderPartial('/user/password', array('model' => $model),false,true);

    }






also u can try


public function actionPassword($id) {




		$model=Users::model()->findByPk((int)$id);

		$model->setScenario('changePassword');

		if (isset($_POST['User']) && !empty($_POST['User'])) {

			if((count(CJSON::decode(CActiveForm::validate($model)))>0)) {

				$this->render('/user/password',array(

						'model'=>$mode,

				));

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

			}

			$modelForm->password_repeat = $data['User']['password_repeat'];


			$model->password=md5($modelForm->password_repeat);

			$model->save(false);




			if (!$model->hasErrors()) {

					

				Yii::app()->user->setFlash('success', "Password change successfully!");

				//$this->redirect(array('admin','id'=>$id));

				$this->redirect(array('admin'));

			}else {

				Yii::app()->user->setFlash('error', "Password change failure!");

				//$this->redirect(array('admin','id'=>$id));

				$this->redirect(array('admin'));

			}





			$this->renderPartial('/user/password', array('model' => $model),false,true);

		}







	}

ok u can try this code


public function actionPassword($id) {


        $user = app()->user->getUser();

        if (isset($user->id) && $user->id === $id || app()->user->isAdmin()) {

            $model = $this->loadModel($id);

            $model->setScenario('changePassword');

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

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

                if ($model->validate()) {

                    $model->password = crypt($_POST['User']['pass1'], Randomness::blowfishSalt());

                    if ($model->save()) {

                        app()->user->setFlash('success', 'Saved new password!');

                        $this->redirect(array('user/setting'));

                        //$this->redirect(array('update', 'id' => $model->id));

                        //$this->redirect('view');

                    }else{

				echo "Ssss" ; die;

			}

                }

            }


            //$this->render('/user/password', array('model' => $model));

//            $this->renderPartial('/user/password', array('model' => $model),false,true);

        } else {

            // access denied for this user

            throw new CHttpException(403, 'Access Denied.');

        }

         $this->renderPartial('/user/password', array('model' => $model),false,true);

    }


    

Again it showing another page. I need same page when i click submit without enter any fields. pls help me.

ok it’s display from error or not?

if u enter the correct password it’s working or not?

yes, when i enter correct password it’s working and it didn’t go any page this fine, but when i enter wrong password it showing another page with error like this “fill all the fields”.

can u post page image when enter a password?(your password layout)

sir, it showing Current Password cannot be blank. like this . i think its not a error. But, Current Password cannot be blank that one also i need show in same page.

Fields with * are required.

Current Password * Current Password cannot be blank.

New Password * New Password cannot be blank.

Confirm Password * Confirm Password cannot be blank.

like this it showing in new page.

first u can check the password in your db using setflash messages so u can change the actionpassword


public function actionChangePassword(){

		$id=Yii::app()->customer->id;

		$model=Users::model()->findByPk((int)$id);

		

		if(isset($_POST['user']['oldpassword']) && !empty($_POST['user']['oldpassword']))

		{

			$model->oldpassword=md5($_POST['user']['oldpassword']);

			if($model->oldpassword!=$model->password){

				Yii::app()->user->setFlash('error',Yii::t("messages",'please insert a correct  old password'));

				$this->redirect('user/password',array('model'=>$modelForm));

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

			}else{

				$model->password_repeat=$_POST['user']['password_repeat'];

				$model->password=md5($modelForm->password_repeat);

				$model->save(false);

				if (!$model->hasErrors()) {

					Yii::app()->user->setFlash('error',Yii::t("messages",'Password change a Succesfully..!'));

					$this->redirect('user/password',array('model'=>$model));

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

				}else{

					Yii::app()->user->setFlash('error',Yii::t("messages",'Password change a Failure..!'));

					$this->redirect('user/password',array('model'=>$model));

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

				}

			}


		}

		$this->render('user/password',array('model'=>$model));

	}



and add the password page (it’s shuold be set flash message display)


<?php

                foreach(Yii::app()->admin->getFlashes() as $key => $message) {

                    echo '<div class="flash-' . $key . '">' . $message . "</div>\n";

                }

                ?>

When i did like this my Change Password link(action) is not working.

ya so please change the name of actionpassword not in actionchangepassword

i changed it but not working action.

Dear sir, actually the problem is when i enter correct password and when i click submit the new password is saving to my database and it didn’t show any new page. But, when i enter wrong password or without enter any password then i click submit at that time only it showing new page with the message like “fill all fields”. But, i need this message also in same page but not in new page.

pls help me. Thank you.