0

I am trying to get a value back through if it found value in database sends/echo result value in it does found sends back/echo 'not_found'.

When I try to compare in the following script it always goes inside if, and never goes to else.

I also tried NULL, FALSE instead not_found does not work.

function showHint(str) {
    var xmlhttp;
    var url=  "check_album.php?q="+str;
    document.getElementById("txtHint1").innerHTML=(url);
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    } else {
        // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            if(xmlhttp.readyState.responseText !='not_found') {
                document.getElementById("txtHint2").innerHTML=xmlhttp.responseText;
            } else {
                document.getElementById("txtHint3").innerHTML='no result';}
            }
        }
        xmlhttp.open("GET",url,true);
        xmlhttp.send();
    }
}

and this is check_album.php code which sending result back

    require_once('../php/classes/class.add_album.php');
    if ($_SERVER["REQUEST_METHOD"] == "GET") {
       $album_name = $_GET["q"];

      //make new object
      $objfile = new add_album();

      //call object method with post method value we got and save result in result 
      $file_found=$objfile->find_album($album_name);

      if ($file_found)echo $file_found;
      else         echo 'not_found';
     }
2
  • 1
    What on Earth is this meant to be ~ xmlhttp.readyState.toString(responseText)? Commented Jun 4, 2014 at 7:28
  • 1
    Also, your app would make much more sense if it simply returned a 404 error status for not found. Commented Jun 4, 2014 at 7:30

2 Answers 2

2

Try this.

 if(xmlhttp.responseText !='not_found' ){
        document.getElementById("txtHint2").innerHTML=xmlhttp.responseText;
  } else
  {
        document.getElementById("txtHint3").innerHTML='no result';
  }
Sign up to request clarification or add additional context in comments.

2 Comments

it is not working for me i have added the code sending the result back please check
@palsikh what does alert(xmlhttp.responseText); show you?
1

This is because response text may have unwanted spaces Try to trim that response

String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g,"");
};

if(xmlhttp.responseText.trim() !='not_found' ){
    document.getElementById("txtHint2").innerHTML=xmlhttp.responseText;
}else{
    document.getElementById("txtHint3").innerHTML='no result';
}

3 Comments

Rajavel D , i have copy-pasted 'not_found' from the file returning it as ajax result so there is no possibility for and space thats why i am using underscore as will
and i have added the file which makes result so please have a look
Try to alert the xmlhttp.responseText before the if condition and let us know the data

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.