Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing two identical strings in JQuery returns false

I discovered an interesting problem with an echoed value from an AJAX request of JQuery, which I don't have an answere for:

My data == "LOCKED" never returns true (line 13)!

JQuery -> AJAX call on button-click:

$.ajax({
        url: "ajax/login_ajax_call.php",
        method: "POST",
        data: { user: usr, password: pwd }
    }).done(function(data){
            if(data == true || data == "true"){          // -> this works with data beeing true (bool) or "true" (string)
                $("#form_submit").submit();
            }
            else{
                console.log(jQuery.type(data));          // -> (string)
                console.log(data);                       // -> "LOCKED" 
                console.log(jQuery.type("LOCKED"));      // -> (string)
                if(data == "LOCKED"){                    // also tried "===" but it never returns true
                    [...]            
                }
                else{
                    [...]
                }
            }
    }); 
});             

PHP(1) -> gets a value returned by a Class(PHP(2)):

include_once("../Classes/Login_check.php");
$lih = new Login_check();
$result = $lih -> check($_POST["user"], $_POST["password"]);
var_dump($result);                                     // -> string(6) "LOCKED"
echo $result;

PHP(2, "Login_check.php"):

[...]
// also tried: 
// $test = "LOCKED"; 
// var_dump($test);                                    // -> string(6) "LOCKED"
// return $test;
return "LOCKED";
[...]

Tell me if you need further informations! I hope anyone know what causes this problem!

like image 262
Y.Hermes Avatar asked Dec 05 '25 23:12

Y.Hermes


1 Answers

Must be because of some white-spaces or new lines, it is always better to trim the data. Try this:

if (data.trim() == "LOCKED") {

You can also use:

if ($.trim(data) == "LOCKED") { // using jQuery.
like image 64
Praveen Kumar Purushothaman Avatar answered Dec 07 '25 17:12

Praveen Kumar Purushothaman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!