0

I have to check weather invoice number is duplicate or not for that i am using following ajax.

function check_duplicate_invoice(num){
          var isDuplicate ;

          xmlhttp = new XMLHttpRequest();
           xmlhttp.open("GET","check_duplicate_invoice.php?in="+num, true);
           xmlhttp.send();
           xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

                    isDuplicate =xmlhttp.responseText.trim() // reponceText will be 0 or 1
                }
            }

            alert(isDuplicate); //result undefined

            if(isDuplicate== 1){
                alert("Invoice Number Already Exist");
            }
    }

I am not able to store ajax output into isDuplicate variable. Please help.

5
  • Did you try to alert xmlhttp.responseText.trim() in the first place to see if it has a value at all? Commented Sep 19, 2015 at 1:22
  • you should use alert(isDuplicate) inside the success function. in your case. the xmlhttp.readyState == 4... Commented Sep 19, 2015 at 1:23
  • @roullie Could be me, but since he's declaring the variable outside the function, but within the same function as the alert, that shouldn't matter. Commented Sep 19, 2015 at 1:26
  • If you are using jQuery you can make you life easier, if you use $.ajax or $.get methods Commented Sep 19, 2015 at 1:29
  • @icecub It does matter, since he is calling that alert before that AJAX request returns the data. That's why isDuplicate is undefined. Commented Sep 19, 2015 at 1:34

1 Answer 1

1

That's because ajax calls are asynchronous. You are looking at the variable before the request has had a time to complete. Try this:

function check_duplicate_invoice(num){
      var isDuplicate ;

      xmlhttp = new XMLHttpRequest();
       xmlhttp.open("GET","check_duplicate_invoice.php?in="+num, true);
       xmlhttp.send();
       xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

                isDuplicate =xmlhttp.responseText.trim() // reponceText will be 0 or 1
                alert(isDuplicate); //result undefined

                if(isDuplicate== 1){
                   alert("Invoice Number Already Exist");
                }

            }
        }

}
Sign up to request clarification or add additional context in comments.

6 Comments

Tried your answer its working in firefox but not in chrome browser. I have to use this in crome. Please help in this matter
what happens in Chrome?
Nothing happens in Chrome
Something would definitely be happening in chrome. Open the developer console and check for errors. Post the code that calls this method (update your question with that code)
I dont know how to use developer console for finding errors. Can you please help
|

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.