Now when I have a user cast a vote the script updates my database but it won't display the following code below to tell the user its vote has been excepted everything else works correctly except my AJAX code.
How can I fix this problem to get the below code to display the new rating when user enters his or her vote?
I'm using PHP
Here is the JavaScript code.
function vote(id, rating) {
if (window.XMLHttpRequest) {
http = new XMLHttpRequest();
} else if (window.ActiveXObject) {
http = new ActiveXObject("Microsoft.XMLHTTP");
}
var url = 'ajax.php?';
var fullurl = url + 'id=' + id + '&rating=' + rating;
//This will create the request to our file, with the information about the vote.
http.open("GET", fullurl, true);
http.send(null);
http.onreadystatechange = statechange_rate;
}
function statechange_rate() {
if (http.readyState == 4) {
var xmlObj = http.responseXML;
var html = xmlObj.getElementsByTagName('result').item(0).firstChild.data;
var id = xmlObj.getElementsByTagName('result').item(0).getAttribute("id");
var votes = xmlObj.getElementsByTagName('result').item(0).getAttribute("votes");
var rating = xmlObj.getElementsByTagName('result').item(0).getAttribute("rating");
//Before, you may have noticed we set votes="-1" if they had already voted, this was just to provide an easy way to check the return of our ajax.php script.
if(votes != -1) {
//This will inform the user about the vote they have cast.
document.getElementsByName('output_' + id).item(0).innerHTML = "<br />" + html;
//This will set a delay to make that message go away in 5000 miliseconds (5 seconds).
window.setTimeout("document.getElementsByName('output_" + id + "').item(0).innerHTML = '';", 5000);
//This will update the rating on the page to the new one.
document.getElementsByName('rating_' + id).item(0).innerHTML = rating;
document.getElementsByName('votes_' + id).item(0).innerHTML = votes;
}else{
document.getElementsByName('output_' + id).item(0).innerHTML = "<br />" + html;
window.setTimeout("document.getElementsByName('output_" + id + "').item(0).innerHTML = '';", 5000);
}
}
}