Lorsqu'un utilisateur émet un vote, le script met à jour ma base de données mais il n'affiche pas le code suivant pour indiquer à l'utilisateur que son vote a été exclu.Problème PHP et AJAX?
//This will output the movie id, new rating, new votes, and a message.
echo "<result id='".$id."' rating='".$rating."' votes='".$votes."'>Vote cast and saved.</result>n";
Comment puis-je résoudre ce problème pour obtenir le code ci-dessus pour afficher lorsqu'un utilisateur entre ou elle son vote?
Voici une partie du code ci-dessous je pense est le problème.
if(mysql_num_rows(mysql_query("SELECT * FROM `voters` WHERE `id`='".$id."' && `ip`='".$ip."'")) == 0) {
//This will insert the information about the user, so they can't vote for the same movie again.
mysql_query("INSERT INTO `voters`(`id`, `ip`) VALUES('".$id."', '".$ip."')");
//This will add one more vote and add the rating to the total rating.
mysql_query("UPDATE `movies` SET `votes`=votes+1, `rating`=rating+".$vote_cast." WHERE `id`='".$id."'") or die(mysql_error());
//This will retrieve the newly updated data about the movie.
$data = mysql_fetch_array(mysql_query("SELECT * FROM `movies` WHERE `id`='".$id."'"));
//This will get the average rating and round it to one decimal place.
$rating = round($data['rating']/$data['votes'], 1);
$votes = $data['votes'];
//This will change the output type to XML, instead of HTML.
header('Content-Type: text/xml');
header('Pragma: no-cache');
//Required header in valid XML files
echo '<?xml version="1.0" encoding="UTF-8"?>'."n";
//This will output the movie id, new rating, new votes, and a message.
echo "<result id='".$id."' rating='".$rating."' votes='".$votes."'>Vote cast and saved.</result>n";
}else{
////This will change the output type to XML, instead of HTML.
header('Content-Type: text/xml');
header('Pragma: no-cache');
////Required header in valid XML files
echo '<?xml version="1.0" encoding="UTF-8"?>'."n";
//This message will be shown if they have already voted,
echo "<result id='".$id."' rating='-1' votes='-1'>You have already voted.</result>n";
}
}
Bon peut-être est cette partie de mon code Ajax ci-dessous qui me donne le problème.
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 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);
}
}
}
@mAdCoDeR Votre PHP semble envoyer l'amende XML, mais le problème est probablement avec votre appel ajax JavaScript à la page PHP. Pouvez-vous s'il vous plaît poster ce code? (Assurez-vous de mentionner si vous utilisez quelque chose comme jQuery ou Prototype) –