/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] AJAX Variable Scope

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”;
}
}
[/code]

Note: I have checked the ajax call and it does return an integer.

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@KorFeb 21.2008 — It is not an integer, as it is a string... But the comparison will work, as it is a comparison by value == not by value&type ===.

Now, at a first glance, my advice is to check also the HTTP channel status
<i>
</i>if(xmlHttp.readyState == 4) {
[COLOR="Blue"]if(xmlHttp.status==200){[/COLOR]
numComments = xmlHttp.responseText;
[COLOR="Blue"]}[/COLOR]
}
Copy linkTweet thisAlerts:
@aj_nscauthorFeb 21.2008 — Nope, hold on, I thought it was working, it's not.
Copy linkTweet thisAlerts:
@aj_nscauthorFeb 21.2008 — Basically, when I click a link, the toggleComments event is fired and a box is displayed showing all the comments of a particular photo. At this time, I can delete comments, which works fine. The problem is, when I click on the toggleComments link again (to hide the comments box) an AJAX request is made to get the number of comments (which has changed since the box was displayed if I deleted one or two) so now the new toggleComments link would show X number of comments.

This doesn't happen and the link shows the same number of comments that was there before I deleted any. If I toggle it again though (on and then off) then the correct number is displayed. Hopefully this isn't too confusing.

Ok, so here's my full code that I am working with:

<i>
</i>var numComments;
function getNumComments(mid) {
//make an ajax call to find out how many comments
//are associated with this mid in the comments table
xmlHttpComments=GetXmlHttpObject(); //function is in Xjslibrary.js
if (xmlHttpComments==null) { return; }

<i> </i>url = "scripts/php/admin/comments.php?numcomments=&amp;mid="+mid;

<i> </i>xmlHttpComments.onreadystatechange = function() {
<i> </i> if(xmlHttpComments.readyState == 4 &amp;&amp; xmlHttpComments.status == 200) {
<i> </i> numComments = xmlHttpComments.responseText;

<i> </i> if(numComments == 0) { //remove the whole comments div for this mid, if any
<i> </i> if(xGetElementById("comments"+mid)) {
<i> </i> xGetElementById("organizer").removeChild(xGetElementById("comments"+mid));
<i> </i> }

<i> </i> }

<i> </i> return numComments;
<i> </i> }
<i> </i>}

<i> </i>xmlHttpComments.open("GET",url,true);
<i> </i>xmlHttpComments.send(null);
}


function toggleComments(mid) {
numComments;
commentsBox = xGetElementById("comments"+mid);

<i> </i>getNumComments(mid);

<i> </i>if(numComments == 1) {
<i> </i> comments = "comment";
<i> </i>} else {
<i> </i> comments = "comments";
<i> </i>}

<i> </i>//more code down here....not important
}
Copy linkTweet thisAlerts:
@aj_nscauthorFeb 21.2008 — I solved the problem by calling getNumComments whenever I delete a comment. That updates the global variable before it is needed again and returns the right variable.
Copy linkTweet thisAlerts:
@KorFeb 21.2008 — when using AJAX it would be a wise thing to declare the globals as [B]null[/B] valued at the beginning and whenever you want to repeat the request. That will save a lot of the CPU's effort as well.
×

Success!

Help @aj_nsc spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...