/    Sign up×
Community /Pin to ProfileBookmark

Multiple Ajax Requests

Hi, I am trying to send 2 ajax requests in the same html web page.

The first(findteams.php) works and the second(submitPick) will send, but not receive any data.

Here is my code:

[CODE]
<script type=”text/javascript”>
function getXMLHTTP() {
var xmlhttp=false;
try{
xmlhttp=new XMLHttpRequest();
}
catch(e) {
try{
xmlhttp= new ActiveXObject(“Microsoft.XMLHTTP”);
}
catch(e){
try{
xmlhttp = new ActiveXObject(“Msxml2.XMLHTTP”);
}
catch(e1){
xmlhttp=false;
}
}
}

return xmlhttp;
}

function getTeams(strURL) {

var req = getXMLHTTP();

if (req) {

req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if “OK”
if (req.status == 200) {
document.getElementById(‘displayTeams’).innerHTML=req.responseText;
} else {
alert(“There was a problem while using XMLHTTP:n” + req.statusText);
}
}
}
req.open(“GET”, strURL, true);
req.send(null);
}

}

function submitPick()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById(“myDiv”).innerHTML=xmlhttp.responseText;
}
}

var home = document.getElementById(‘home’).options[document.getElementById(‘home’).selectedIndex].value;
var away = document.getElementById(‘away’).options[document.getElementById(‘away’).selectedIndex].value;
var score = document.getElementById(‘score’).options[document.getElementById(‘score’).selectedIndex].value;
var queryString = “&score=” + score + “&home=” + home + “&away=” + away;

xmlhttp.open(“GET”, “update.php” + queryString,true);
xmlhttp.send();
}

</script>

[/CODE]

They are both being sent asynchronously, so in theory should work.

Can someone help me spot the problem here?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@astupidnameMay 14.2011 — var queryString = "[COLOR=red]&[/COLOR]score=" + score + "&home=" + home + "&away=" + away;

xmlhttp.open("GET", "update.php" + queryString,true);
[/QUOTE]


The problem there is an improperly built queryString which begins with '&' when it should begin with '?', so change that around and should work fine then.

At least, that is, if home away & score values don't contain any special characters, if so you may want to use encodeURIComponent() on them
Copy linkTweet thisAlerts:
@inplaytodayauthorMay 15.2011 — thanks asn, hadn't spotted that.

after changing queryString and ensuring that there are no special characters in home, away or scores, the div "myDiv" will still not update.

This should be a simple process to complete. I've been looking at this thing from every direction and tried everything I can think of. Please help!
Copy linkTweet thisAlerts:
@inplaytodayauthorMay 15.2011 — was just thinking...is it even possible to collect data from an AJAX div that was updated on the same page and subsequently perform another AJAX request with that data?
×

Success!

Help @inplaytoday 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.18,
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,
)...