2

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!

Y.Hermes
  • 449
  • 5
  • 22

3 Answers3

9

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.
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
2

You can try JSON.parse. If value looks like "LOCKED" on console, it means its like ""LOCKED"".

Edit 1

As commented, you are getting "\r\nLOCKED" as stringified value, so you can use a regex to replace it. Following is the updated code.

Reference - How to remove all line breaks from a string?

Sample

var a = "LOCKED";
var strA = "\r\nLOCKED";
var regex = /\r?\n|\r/g;
var cleanStr = strA.replace(regex,'');
console.log(a);
console.log(strA);

console.log(strA == a);
console.log(cleanStr == a);
Community
  • 1
  • 1
Rajesh
  • 24,354
  • 5
  • 48
  • 79
  • Gets me a way nearer to the result: With a stringify it returns: "\r\nLOCKED" – Y.Hermes Jan 12 '16 at 13:23
  • 1
    Would get a second +1 from me but, you are right, it is solving the problem (but, that results out of another probleme). If I won't find the first mistake, i'll accept that answere, thanks a lot! – Y.Hermes Jan 12 '16 at 13:34
  • 1
    Ideally you should find why are you receiving such result from server and send it in proper format. – Rajesh Jan 12 '16 at 13:46
1

Probably due to whitepace somewhere, such as a space before an opening php tag etc.

Far better to use a structured data format like json, and return a boolean propery to avoid these issues:

$result = $lih -> check($_POST["user"], $_POST["password"]);
$locked = ($result == 'LOCKED');
header('Content-Type: application/json');
echo json_encode(['locked'=>$locked]);

Javascript:

if(data.locked){ 
Steve
  • 20,703
  • 5
  • 41
  • 67