2009-12-27 71 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