0


this is my problem. I have this code that allows me to check via ajax call if the "oldPsw" value is already present in the database. Based on the result, the internal control updates the input and error field cssrelative. And all this works well.

    var errOld  = 0;
    var oldPsw  = $('#oldPsw').val();
    $.ajax({
        url:    "js/psw.php",
        data:   {'psw': oldPsw, 'user': user},
        type:   "GET",
        success:function(data){
            var ctrl_oldPsw = jQuery.parseJSON(data);
                div = $('#oldPsw').parent();
            if ( oldPsw.length === 0 ) {                            // empty value
                div.find('#oldPsw').removeClass().addClass('error');
                div.find('.icon-check').html('<i class="far fa-times-circle"></i>');
                div.find('.span-error').html('Campo obbligatorio').removeClass('hidden');
                errOld = 1;
            } else if ( ctrl_oldPsw.check == 'false' ) {            // old psw don't matches
                div.find('#oldPsw').removeClass().addClass('error');
                div.find('.icon-check').html('<i class="far fa-times-circle"></i>');
                div.find('.span-error').html('Inserire la password attuale').removeClass('hidden');
                errOld = 1;
            } else {                                                // psw matches
                div.find('#oldPsw').removeClass().addClass('correct');
                div.find('.icon-check').html('<i class="far fa-check-circle"></i>');
                div.find('.span-error').addClass('hidden');
            }
        }
    })

My problem is that the variable "errOld" instead does not update its value. It should return 0 in case the password matches or go to 1 in case of error, but it always returns only 0 even if the code next to it works.
Can you tell me what escapes me?
PS. I specify that it does not update the variable, in fact if at the first statement I do not give value it returns "undefined"

  • Welcome to Stack Overflow. In your `if()` statement, I see two that update `errOld` and one that does not. So if yoolur first two conditions are not met `errOld` will not get update. I suspect that this is what is happening. – Twisty May 12 '20 at 17:25
  • Where *exactly* are you checking the value? directly after `$.ajax(`? Sounds like you're not waiting for it to be updated asynchronously. – freedomn-m May 12 '20 at 17:25
  • Similar to duplicate: https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – freedomn-m May 12 '20 at 17:27
  • Thanks for the quick response. The last else does not need to update the variable as it takes care of setting the css in case there are no errors, so errOld in that case it is right it remains 0, my problem is that in both the first two cases of the if the css updates but errOld no. And no, the update takes place only after the success of the call. – Danilo Sonnante May 12 '20 at 17:34
  • I would suggest adding a `console.log(oldPsw.length)` to each of the if statements that helps you identify which condition is being met, – Twisty May 12 '20 at 18:17

0 Answers0