0

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;
   ?>
   */
3
  • 2
    It probably means that the server does not return match. Outside of that if statement, try alert(ret);. What do you get? Commented Mar 22, 2016 at 20:19
  • 1
    done any basic debugging? 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 into ret, so if your code happens to issue a PHP warning, that warning becomes part of ret. Commented Mar 22, 2016 at 20:20
  • I will try to debug next time. I'm still very green . Thanks Commented Mar 22, 2016 at 20:34

1 Answer 1

1

Your PHP code has possibly output whitespace around match. Try this

if (ret.trim() == "match"){
Sign up to request clarification or add additional context in comments.

1 Comment

Other thing is that i wanted to follow this by document.getElementById().innerHTML = "" to clear out field in html, but it doesn't work too ??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.