Wednesday, April 12, 2017

ajax call in submit handler

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 = "";

 $(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";
    }`



via Swati

Advertisement