Reputation: 15198
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?
Upvotes: 1
Views: 653
Reputation: 15198
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
}
}
Upvotes: 0
Reputation: 6181
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.
Upvotes: 1