Ever since I’ve started developing applications with ajax I’ve been having this problem but I’ve always found a way around it so I’ve never had to find a solution. I’m tired of that and I need one now.
How come in the below script, numComments is undefined in the rest of my function below the ajax call and what can I do to make define it within the ajax function?
[code]
function getNumComments(mid)
var numComments;
commentsBox = xGetElementById(‘comments’+mid);
//make an ajax call to find out how many comments
//are associated with this mid in the comments table
xmlHttp=GetXmlHttpObject(); //function is in Xjslibrary.js
if (xmlHttp==null) { return; }
url = “scripts/php/admin/comments.php?numcomments=&mid=”+mid;
xmlHttp.onreadystatechange = function() {
if(xmlHttp.readyState == 4) {
numComments = xmlHttp.responseText;
}
}
xmlHttp.open(“GET”,url,true);
xmlHttp.send(null);
if(numComments == 1) {
comments = “comment”
} else {
comments = “comments”;
}
}
Note: I have checked the ajax call and it does return an integer.