Mike Whiskey
Mike Whiskey

Reputation: 3

Javascript AJAX PHP if statement not working

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;
   ?>
   */

Upvotes: 0

Views: 38

Answers (1)

Chris
Chris

Reputation: 5876

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

if (ret.trim() == "match"){

Upvotes: 1

Related Questions