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.
xmlhttp.responseText.trim()in the first place to see if it has a value at all?alert(isDuplicate)inside the success function. in your case. thexmlhttp.readyState == 4...jQueryyou can make you life easier, if you use$.ajaxor$.getmethodsalertbefore that AJAX request returns the data. That's whyisDuplicateis undefined.