/    Sign up×
Community /Pin to ProfileBookmark

request ajax multiple with onstatechange

hi ok i have a problem im abit of noob but im trying to get this working

whats ment to happen is all three classnames should change but they dont, i tried this code thinking the for loop would go through 1-130 checking the .onreadystatechange but firefox error console gives me this error

btw there isnt 3 div tags like this ive just cut them it changes depending on page so i need a fix that will work from 1-130

Error: AjaxResponseArray[i] is undefined
Source File: [url]http://127.0.0.1/lib/js/trophies.js?v=1244586482[/url]
Line: 42

but it shouldnt get to line 42 because i have a if function testing if undefined, im working with Firefox 3.0.0.10

Thanks in Advance

HTML onclick code if i click these one after another they all do there job but only the last one has its classNames changed

[code=html]<a href=”http://127.0.0.1/page/trophies/update.php?c=e4da3b7fbbce2345d7772b0674a318d5&gid=68&tid=2304&refer=http%3A%2F%2F127.0.0.1%2Fpage%2Ftrophies%2Fadd.php%3Fuid%3D5%26gid%3D68%26c%3De4da3b7fbbce2345d7772b0674a318d5″ onClick=”update_trophy(‘2304′,’68’, ‘e4da3b7fbbce2345d7772b0674a318d5’);return false;”>
<div id=”div2304″ class=”trophy-got” title=”Score at least 10 goals in one game.” alt=”Score at least 10 goals in one game.”>
<div id=”divimage2304″ class=”image-got”>
<img src=”http://127.0.0.1/lib/images/trophies/68/2304.png?v=1240083230″ title=”Score at least 10 goals in one game.” name=”stopload” alt=”Score at least 10 goals in one game.” id=”image2304″/>
</div>
<div class=”text-got”>

<h3 id=”h32304″>Score Big</h3>
<h4 id=”h42304″>Score at least 10 goals in one game.</h4>
</div>
<div class=”type-got”>
<img src=”http://127.0.0.1/lib/images/tick.gif” id=”type2304″ style=”width:16px;height:16px;margin:4px”/>
</div>
</div>
</a>

<a href=”http://127.0.0.1/page/trophies/update.php?c=e4da3b7fbbce2345d7772b0674a318d5&gid=68&tid=2305&refer=http%3A%2F%2F127.0.0.1%2Fpage%2Ftrophies%2Fadd.php%3Fuid%3D5%26gid%3D68%26c%3De4da3b7fbbce2345d7772b0674a318d5″ onClick=”update_trophy(‘2305′,’68’, ‘e4da3b7fbbce2345d7772b0674a318d5’);return false;”>
<div id=”div2305″ class=”trophy-got” title=”Score two goals with a Big Player powerup in one game.” alt=”Score two goals with a Big Player powerup in one game.”>
<div id=”divimage2305″ class=”image-got”>
<img src=”http://127.0.0.1/lib/images/trophies/68/2305.png?v=1240083230″ title=”Score two goals with a Big Player powerup in one game.” name=”stopload” alt=”Score two goals with a Big Player powerup in one game.” id=”image2305″/>
</div>
<div class=”text-got”>
<h3 id=”h32305″>Big Player</h3>
<h4 id=”h42305″>Score two goals with a Big Player powerup in one game.</h4>

</div>
<div class=”type-got”>
<img src=”http://127.0.0.1/lib/images/tick.gif” id=”type2305″ style=”width:16px;height:16px;margin:4px”/>
</div>
</div>
</a>
<a href=”http://127.0.0.1/page/trophies/update.php?c=e4da3b7fbbce2345d7772b0674a318d5&gid=68&tid=2306&refer=http%3A%2F%2F127.0.0.1%2Fpage%2Ftrophies%2Fadd.php%3Fuid%3D5%26gid%3D68%26c%3De4da3b7fbbce2345d7772b0674a318d5″ onClick=”update_trophy(‘2306′,’68’, ‘e4da3b7fbbce2345d7772b0674a318d5’);return false;”>
<div id=”div2306″ class=”trophy-got” title=”Score two goals with a Super Speed powerup in one game.” alt=”Score two goals with a Super Speed powerup in one game.”>
<div id=”divimage2306″ class=”image-got”>

<img src=”http://127.0.0.1/lib/images/trophies/68/2306.png?v=1240083230″ title=”Score two goals with a Super Speed powerup in one game.” name=”stopload” alt=”Score two goals with a Super Speed powerup in one game.” id=”image2306″/>
</div>
<div class=”text-got”>
<h3 id=”h32306″>Super Speed</h3>
<h4 id=”h42306″>Score two goals with a Super Speed powerup in one game.</h4>
</div>
<div class=”type-got”>
<img src=”http://127.0.0.1/lib/images/tick.gif” id=”type2306″ style=”width:16px;height:16px;margin:4px”/>

</div>
</div>
</a>
[/code]

AJAX CODE

[code=html]
var AjaxResponseArray = new Array()
var AjaxUrlArray = new Array()
var AjaxTidArray = new Array()
var AjaxGidArray = new Array()
var index=0;

function update_trophy(tid, gid, code){
index++;
// We got one
try{
AjaxResponseArray[index] = new XMLHttpRequest();
}
catch(e){
try{
AjaxResponseArray[index] = new ActiveXObject(“Msxml2.XMLHTTP”);
}
catch (e2){
try{
AjaxResponseArray[index] = new ActiveXObject(“Microsoft.XMLHTTP”);
}
catch (e){
// no AJAX support
}
}
}

AjaxUrlArray[index]=site_url+”page/trophies/update.php?gid=”+gid+”&tid=”+tid+”&c=”+code+”&refer=”+site_url+”&aj=1″
AjaxUrlArray[index]=AjaxUrlArray[index]+”&sid=”+Math.random()
AjaxTidArray[index]=tid
AjaxGidArray[index]=gid

AjaxResponseArray[index].open(“GET”,AjaxUrlArray[index],true)
for (i=0;i<=130;i++){
if (AjaxResponseArray[i] != null || AjaxResponseArray[i] != undefined){
AjaxResponseArray[i].onreadystatechange = CheckResponse;
}
}
AjaxResponseArray[index].send(null)
}

function CheckResponse(){
AjaxResponseArray[i].onreadystatechange = function() {
if(AjaxResponseArray[i].readyState == 4 || AjaxResponseArray[i].readyState == “complete”){
document.getElementById(“dump”).innerHTML=AjaxResponseArray[i].responseText+” index: “+index;
document.getElementById(“image”+AjaxTidArray[i]).src=site_url+”lib/images/trophies/”+AjaxGidArray[i]+”/”+AjaxTidArray[i]+”.png”;

if(AjaxResponseArray[i].responseText == “Removed”){
//if not got
document.getElementById(“div”+AjaxTidArray[i]).className=”trophy”;
document.getElementById(“divimage”+AjaxTidArray[i]).className=”image”;
document.getElementById(“h4″+AjaxTidArray[i]).style.display=”block”;
document.getElementById(“type”+AjaxTidArray[i]).style.display=”none”;

}else{
//if got
document.getElementById(“div”+AjaxTidArray[i]).className=”trophy-got”;
document.getElementById(“divimage”+AjaxTidArray[i]).className=”image-got”;
document.getElementById(“h4″+AjaxTidArray[i]).style.display=”none”;
document.getElementById(“type”+AjaxTidArray[i]).style.display=”none”;
}

}else{
//document.getElementById(“loading”).style.display=”block”;
document.getElementById(“dump”).innerHTML=”Loading…”+” index: “+index;
document.getElementById(“image”+AjaxTidArray[i]).src=site_url+”lib/images/loading.gif”;
}
}
}[/code]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@A1ien51Jun 09.2009 — You have nothing at index zero. You do index++ right away so 0 never is used, it goes right to 1.

Eric
Copy linkTweet thisAlerts:
@gavshouseauthorJun 10.2009 — You have nothing at index zero. You do index++ right away so 0 never is used, it goes right to 1.

Eric[/QUOTE]


No still gives a error, but it should give this error because i check for it

Error: AjaxResponseArray[i] is undefined

Source File: http://127.0.0.1/lib/js/trophies.js?v=1244638570

Line: 42
Copy linkTweet thisAlerts:
@A1ien51Jun 10.2009 — okay your code is worse off than I expected.

You are going to have major issues with your variable i

Plus you are not assigning the onreadystatechange correctly in the first sections and somehow you are redefining in in your other fucntion with a variable that probably does not exist.

I am not going to wade through your code to fix it since I am on a deadline today. I would HIGHLY recommend you dump your own Ajax code and pick up JQuery or Prototype.js and use theirs. This way you will not have to worry about 99% of that code.

Eric
Copy linkTweet thisAlerts:
@gavshouseauthorJun 10.2009 — okay your code is worse off than I expected.

You are going to have major issues with your variable i

Plus you are not assigning the onreadystatechange correctly in the first sections and somehow you are redefining in in your other fucntion with a variable that probably does not exist.

I am not going to wade through your code to fix it since I am on a deadline today. I would HIGHLY recommend you dump your own Ajax code and pick up JQuery or Prototype.js and use theirs. This way you will not have to worry about 99% of that code.

Eric[/QUOTE]


yeah ive tried Jquery and Prototype but cant figure them out
×

Success!

Help @gavshouse 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.19,
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,
)...