I've been struggling with this problem for over two hours now and can find any reasonable solution. If i will get rid of if statement marked by ---> this arrow. alert() on its on will work. It will will be triggered when i just put if(true) but it don't work with following condition. I'm not really good with this languages. Just have to finish that for school project. I'm sure it is something small but cat figure it out on my own.
Java Script Function
function checkLogin(str) {
if (str.length == 0) {
return;
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var ret = xmlhttp.responseText;
----> if (ret == "match"){
alert("Login name already exists !");
}
}
}
xmlhttp.open("GET", "loginCheck.php?q=" + str, true);
xmlhttp.send();
}
}
PHP Code
/*
<?php
include 'db.inc.php';
date_default_timezone_set("UTC");
$login = $_REQUEST["q"];
$sql = "SELECT * FROM staffMembers";
if (!($result = mysqli_query($con,$sql))){
die ('An Error ->' . mysqli_error() );
}
while ($row = mysqli_fetch_array($result)){
$firstName = $row['nameLogin'];
$ret="true";
if($firstName == $login){
$ret = "match";
break;
}
}
mysqli_close($con);
echo $ret;
?>
*/
match. Outside of thatifstatement, tryalert(ret);. What do you get?console.log(ret)and see what's really coming back. Note that JS doesn't understand html, or anything your server spits out. It'll take ANY output and put it intoret, so if your code happens to issue a PHP warning, that warning becomes part ofret.