℡Wang Yan 2017-04-13 05:06 采纳率: 100%
浏览 9

提交处理程序中的ajax调用

In my change password functionality, I use jquery validator for validation and ajax call for checking old password in submit handler .I have tried AJAX in custom rule(addMethod) in jquery for checking old password.It gave me "Incorrect password " and "This field is required " message at the same time for empty password. Then I tried Ajax call in submit handler as below now it applying validation correctly but not checking for old password in Ajax.

var postUrl = "{{ URL::to('frontend/checkpassword') }}";

$(document).ready(function() {
    $('#passwordForm').validate({
        rules: {
            password: {
                required: true,
                password1: {
                    required: true,
                    minlength: 8
                },
                password2: {
                    required: true,
                    equalTo: '#password1'
                },
            },
            messages: {
                password: {
                    required: "Please enter Old Password",
                },
                password1: {
                    required: "Please enter a password",
                    minlength: "Your password must be at least 8 characters long"
                },
                password2: {
                    required: "Please enter a password",
                    equalTo: "The two passwords do not match!"
                },
            },
        },
        submitHandler: function(form) {
            $.ajax({
                type: "POST",
                url: postUrl,
                data: $("#passwordForm").serialize(),
                async: true,
                beforeSend: function() {
                    $("#loading-image").show();
                },
                success: function(response) {
                    if (response == 0) {
                        $("#loading-image").hide();
                        $('#display').html("Incorrect Password");
                        $('#password1, #password2 ').prop('disabled', true);
                    } else {
                        $("#loading-image").hide();
                        $('#display').html("Correct Password");
                        $('#password1,#password2').removeAttr('disabled');
                    }
                }
            });

            return false;
        }
    });
});

My code for checking old password is

$strPassword = $request->password;
$intSessionUserId=Session::get('sessionUserId');

$user = register::find($intSessionUserId);
$strOldPassword = $user->password1;

if($strPassword==$strOldPassword) {
    echo "1";
}
else {
    echo "0";
}
  • 写回答

1条回答 默认 最新

  • 谁还没个明天 2017-04-13 05:44
    关注

    As per mentioned your code in comment

    public function checkPassword(Request $request) { 
        $strPassword = $request->password;
        $intSessionUserId=Session::get('sessionUserId'); 
        $user = register::find($intSessionUserId); 
        $strOldPassword = $user->password1; 
        if($strPassword==$strOldPassword) {
            echo "1"; 
        } else { 
            echo "0"; 
        } 
    }
    

    I think you may be compare your plain password with encrypted password. So please check and confirm which encryption method you are using for encrypt password. and encrypt password which is stored in $strPassword. This may be resolve your issue.

    I see you are using Laravel Framerowk. so you can use below line for password hash comparision.

    if(Hash::check($strPassword, $strOldPassword)){
        echo "1";
    }else {
        echo "0";    
    }
    

    Refer: https://laravel.com/docs/5.0/hashing

    评论

报告相同问题?