/    Sign up×
Community /Pin to ProfileBookmark

function only outputs last values, unless an alert if present

Hi, I am new to the forums and new to Javascript programming. I have come across a problem I just can’t seem to solve. I have an html table with abbreviated data that when it is clicked on, it calls a javascript function to show the full data(which is retrieved from a database using a php script). Each row has a unique id. If I run the code as shown below, it only outputs the data for the last table row. However, if I include an alert right after the xmlHttp.send, it works fine. Any ideas why this is happenin?

Here is the javascript code:

[CODE]function expandLocality(){

var locTable= document.getElementsByTagName(“table”)[0];
//append new headers
var header=locTable.getElementsByTagName(‘tr’)[0];
header.innerHTML=”<th>Taxa</th><th>Country</th><th>State</th><th>Citation</th><th>Locality</th><th>Verbatim Locality</th><th>Lat.</th><th>Long.</th><th>Date</th><th>Type</th><th>Deposition</th>”;
var numRows=locTable.getElementsByTagName(“tr”).length;
for(i=1; i<numRows; i++){
//get the specid for each row then send that to php script to get data
var specRow=locTable.getElementsByTagName(“tr”)[i];
var specid=specRow.id;
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null){
alert (“Your browser does not support AJAX!”);
return;
}

var url=”getLocal.php?specid=”+specid;
xmlHttp.open(“GET”,url,true);
xmlHttp.onreadystatechange=function(){
if (xmlHttp.readyState==4 && xmlHttp.status == 200){
//response text = new data for each td to append to tr
var output=xmlHttp.responseText;
var temp=output.split(‘|’);
for (j=0; j<temp.length; j++){
y=document.createElement(‘td’);
y.appendChild(document.createTextNode(temp[j]));
document.getElementById(specid).appendChild(y);
}

}
}

xmlHttp.send(null);

//alert(); if this is uncommented, script works fine.
}
}
[/CODE]

I appreciate the help!

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@hoangkcSep 17.2007 — you should wait until one row is updated to send the next ajax request. or using an ajax call to get all data, then parse it in client to build table.

here, you send numRows times ajax request, with only xmlhttp.
Copy linkTweet thisAlerts:
@jmw975authorSep 18.2007 — Thanks for the help. I will see what I can do. I guess I don't understand the xmlhttp request fully enough to understand why the rows aren't being updated before the next request is made.


It should be easy enough, however, to get all the data in one php script and then update the table that way, rather than several xmlhttp requests

thanks again!
×

Success!

Help @jmw975 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...