/    Sign up×
Community /Pin to ProfileBookmark

PHP / AJAX / JS refresh two divs with one onClick

Hi,

Need some help please. I have the following function:

[CODE]<script type=”text/javascript”>
function getVote(int)
{
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(“ajax”).innerHTML=xmlhttp.responseText;
}
}

var questionid = document.question.questionid.value;

xmlhttp.open(“GET”,”result.php?id=”+questionid+”&vote=”+int,true);
xmlhttp.send();
}
</script>[/CODE]

I then have the following onClick:

[CODE]<input type=”radio” name=”vote” value=”1″ onClick=”getVote(this.value);” />
<input type=”radio” name=”vote” value=”2″ onClick=”getVote(this.value);” />[/CODE]

Currently, all is good and works perfectly, the div ‘ajax’ refreshes ajax style with ‘result.php’ and I can take the value. But what I now need to do is refresh two divs. I have tried everything from putting two onClicks on the radio button, within the ‘getVote’ function calling in the second function and many more alternatives.

The second function is pretty much the same, just a different div ID:

[CODE]<script type=”text/javascript”>
function getTotal(int)
{
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(“total”).innerHTML=xmlhttp.responseText;
}
}

xmlhttp.open(“GET”,”total.php?id=”+questionid+”&vote=”+int,true);
xmlhttp.send();
}
</script>[/CODE]

The problem I have, I think, is that both functions need to use ‘xmlhttp.open’, and it seems once one has ran, the functions end.

Any ideas gratefully received.

Thanks.

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@tpdietzJan 10.2011 — In the first function, I see:

<i>
</i>var questionid = document.question.questionid.value;


I do not see an assignment like that in the second function.
Copy linkTweet thisAlerts:
@samhale86authorJan 10.2011 — You are correct, thats grabbing a hidden form field I haven't shown in my code, and isn't required on the second refresh. But that isn't the reason it isn't working unfortunately, as I started with it there.
Copy linkTweet thisAlerts:
@samhale86authorJan 10.2011 — I also tried with just;

[CODE]xmlhttp.open("GET","total.php",true);[/CODE]

As I do not need to pass anything through, just refresh the div.
Copy linkTweet thisAlerts:
@janusmccarthyJan 11.2011 — Sam, this is basically just an old ajax styled request (which by the way will cause browser issues because the way browsers handle the ajax requests are browser dependent).

There's two ways to do this.

EITHER you basically want to create a second http request object...

OR Make one request, and parse your return statement to get both value.

Here's why:

Ajax requests happen asynchronously, you make the request and go on to the next statement, while the backend processes it. When you get a reply, you call the onreadystatechange function.

That's why you can't really reuse the request object. You'd be overwriting your callback function.

Since you've got everything written, it's probably best to use the first option. Just make xmlhttp a local variable in both functions, and call the functions one after the other.

So you either need a different request object with a different callback, or you need to get both items back in the same reply. I'd personally go for the second one and use something like JSON, which is supported natively by both php and JS.
Copy linkTweet thisAlerts:
@samhale86authorJan 11.2011 — Hi Janus,

Thanks so much for your reply. Really gave me a heads up on the code.

I'm quite an Ajax dummy, and just found this code through searching Google, so if I can find up-to-date code that will do the trick, I am happy to use it.

Someone on another forum fixed the above code for me, and it seemed to have done the trick. But I've now found a subsequent error in Internet Explorer, that could possibly be down to the code being out of date.

My code is working perfectly in Firefox and Chrome, but in Internet Explorer, although the refreshing seems to happen, the actual file is not being refreshed. For example, I have changed <div id="total"> to print the current time in PHP. This doesn't work in Internet Explorer. It just continues to show the time I first hit that page.

My other div <div id="ajax">, contains PHP to pull in a random record from the database, again, this works in Firefox and Chrome, but in Internet Explorer, it just keep showing me the record when I first hit the page.

Any thoughts?

Thanks again.
Copy linkTweet thisAlerts:
@janusmccarthyJan 11.2011 — Well it's not so much that the code is out of date, but that browsers perform that function in different ways, and MS in particular tends to put version info.

If you want to do it this way, that's fine. Just out of wondering, have you considered using something like jquery? Google will host the code for you, so you only need to add one line of code in your webpage, and it takes the maintenance work out for you and your code instead becomes something like:

[CODE]
$.ajax({
type: "POST",
url: "some.php",
data: "name=John&location=Boston",
success: function(msg){
alert( "Data Saved: " + msg );
}
});
[/CODE]
×

Success!

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