2009-12-27 68 views
1

当用户投票时脚本更新我的数据库,但它不会显示下面的代码来告诉用户其投票已被排除。PHP和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"; 


我怎样才能解决这个问题得到当用户进入她或他的票上面的代码显示?


这里是下面的代码,我认为这是问题的一部分。

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"; 
} 

}


好吧,也许这是低于我的Ajax代码是给我的问题,这部分内容。

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); 
     } 
    } 
} 
+1

@mAdCoDeR您的PHP似乎正在发送XML,但问题可能在于您的JavaScript ajax调用PHP页面。你可以请张贴代码吗? (一定要提及,如果你使用的东西像jQuery或原型) – 2009-12-27 05:12:18

回答

1

我相信你的问题是,你在呼唤data,而不是像textContent

var html = xmlObj.getElementsByTagName('result').item(0).firstChild.textContent; 

然而,只是为了测试一切工作正常(如果没有解决问题) ,做这样的事情:

var html = "Sample content"; 

这样,你可以看到,如果你所有的getElementsByName通话是否正常工作。

是否有一个原因,你没有使用类似jQuery的库来使这个更易于管理和跨浏览器证明?

jQuery使某些事情变得非常简单,例如你的ajax调用看起来像这样(而不是new XMLHTTPRequest和其他代码行)。该idvote是任意的,并会在PHP中引用作为$_POST['id']$_POST['vote']

$.post('/path/to/file.php', {id: "1", vote:5 }, function(data){ 
    // Runs when ajax call successfully returns. data is the xml 
}, "xml"); 

和选择和更新的元素(这将取代你getElementsByName):

$("#output_" + id).html("<br />" + html); // Select by id 

希望帮助解释多一点为什么你可能想要使用jQuery或类似的库来简化你的代码......和你的生活。

+0

从我的理解是不是几乎在所有方面AJAX更好,并与PHP更好可能是错误的? 我会尝试你的答案并回传结果。 – mAdCoDeR 2009-12-27 05:37:21

+0

@mAdCoDeR(你的名字很难打字;)我并不真正理解你的评论,但我所说的只是使用jQuery(一个JavaScript库)可以真正清理你的代码并使其更容易维护。 – 2009-12-27 05:40:01

+0

好的,您的回答给了我与其他解决方案之前相同的结果吗?我的意思是说我认为AJAX更好,但我会研究JQuery。附:剪切和粘贴总是有用的:) – mAdCoDeR 2009-12-27 05:44:12