mardi 10 juillet 2018

Javascript triple equal not working

I am trying to get a return value from a PHP script, and compare it to a set value in Javascript through XMLHttpRequest. I am unsure why the logic statement in my if-statement is false.

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    var count = this.responseText;
    document.getElementById('test1').innerHTML = typeof count;
    document.getElementById('test2').innerHTML = count;
    var ideal = 4;
    var ideal_str = ideal.toString();
    if (count===ideal_str) {
        document.getElementById('test3').innerHTML = 'true';
    } else {
        document.getElementById('test3').innerHTML = 'false';
    }
    };
xhttp.open("POST","check_files.php", true);
xhttp.send();

When I run it I get the following:

document.getElementById('test1').innerHTML = typeof return gives string. document.getElementById('test2').innerHTML = return gives 4. document.getElementById('test3').innerHTML = return gives false.

I have also attempted to use double-equals as well as convert the return to an integer using parseInt().

Aucun commentaire:

Enregistrer un commentaire