1

A new problem emerged from this thread: jquery form validator never makes ajax call, in that the data returned from the ajax request is accurate, but it always fails on a comparison. This is the code that makes the ajax request:

var pass_form = $('#pass_form');
pass_form.submit( valid_pass_sett );

function valid_pass_sett() {
    //remove old errors - snipped
    pass_old = $('input[name=pass_old]').val();
    pass_new = $('input[name=pass_new]').val();
    pass_confirm_new = $('input[name=pass_confirm_new]').val();

    if (pass_old === "") {
        //display error on form - snipped
        return false;
    } else if (pass_new === "") {
        //display error on form - snipped
        return false;
    } else if (pass_new != pass_confirm_new) {
        //display error on form - snipped
        return false;
    } else if (pass_new.length < 8) {
        //display error on form - snipped
        return false;
    } else {
        $.post("http://www.example.com/ajax/validate.php",{ // async validation
            type: 'valid_old_change_pass', 
            pass_old: pass_old,
            pass_new: pass_new
        }, valid_pass_combo_callback);
        alert('after the ajax call...');
    }
    return false;  // cancel form submission
}

this is the code that the request is submitted to:

$username = $_SESSION['username'];
$pass_old = $_POST['pass_old'];
$pass_new = $_POST['pass_new'];
if (empty($pass_old) || empty($pass_new)) {
    echo "invalid";
} else if (!User::valid_user_pass($username, $pass_old)) {
    echo "invalid_old";
} else if (!Sanitize::is_legal_password($pass_new)) {
    echo "invalid_new";
} else {
    echo "valid";
}

and this is the callback function that processes it; the callback function is the one where the comparison always fails.

function valid_pass_combo_callback( data ) {

//breakpoint is set here
    if (data == 'valid') {
        //only if the form is valid!
        pass_form[0].unbind('submit').submit();
    }
    else if (data == "invalid_old") {
        //display error on form - snipped
    }
    else if (data == "invalid_new") {
        //display error on form - snipped
    }
    else {
        //it always jumps to here..., even though data *is* the correct value
    }
}

I debugged this code, and validate.php is returning "invalid_old" which is correct (based on the test data I'm entering). So, data is storing "invalid_old" according to Firebug; however, the code always jumps down to the last else statement. Why is the comparison always failing?

Community
  • 1
  • 1
Ricardo Altamirano
  • 14,650
  • 21
  • 72
  • 105
  • 2
    Try making a break point in the callback and setting a watch for the expression `data == "invalid_old"`. What does it say? I suspect it may be an incorrect case or something like that that's tough to see sometimes. – FishBasketGordo Aug 12 '11 at 17:08
  • 1
    Perhaps your server is returning a line break after `"invalid_old"`. Download the trial version of Charles proxy (an indespensible tool if you're working with ajax) and check the hex view of the data returned by the server. – Matthew Aug 12 '11 at 17:14
  • I didn't use Charles proxy, but I added a `trim` function to validate.php, and that fixed the problem. Thank you so much for all the help! – Ricardo Altamirano Aug 12 '11 at 17:36

2 Answers2

1

Try placing an alert(data.toString()); above your checks temporarily to see what it returns. If it's not the string itself, then it's not returning the data you're expecting.

CassOnMars
  • 6,153
  • 2
  • 32
  • 47
0

As stated in the comment, adding JQuery's trim function fixed the problem, as shown:

function valid_pass_combo_callback( data ) {
    trimmed_data = $.trim(data);
    if (trimmed_data == 'valid') {
        //only if the form is valid!
        pass_form[0].unbind('submit').submit();
    }
    else if (trimmed_data == "invalid_old") {
        //display error on form - snipped
    }
    else if (trimmed_data == "invalid_new") {
        //display error on form - snipped
    }
    else {
        //it always jumps to here..., even though data *is* the correct value
    }
}
Ricardo Altamirano
  • 14,650
  • 21
  • 72
  • 105
  • @StephanKristyn I think this is the code I used. I no longer have access to this code base (different workplace) but I believe this is what I used. – Ricardo Altamirano Nov 09 '12 at 15:51