2010-08-30 120 views
1

我正在研究AJAX和PHP中的投票系統,我遇到了一些麻煩。我們在數據庫中顯示了一堆帖子,每個帖子旁邊都有一個圖片 - 單擊圖片應該1)切換圖片顏色,然後2)使用AJAX調用一個PHP腳本,然後決定是否添加或減去一票。我有圖像切換工作,但我不知道如何做下一部分。什麼是最好的方法來做到這一點?AJAX + PHP投票系統

這是while循環,其輸出所述柱:

while($row = mysql_fetch_array($result)) 

      { 

    ?> 

     <li class = "post"> 
      <a href = "#" onclick = "return toggle(this,'heart<?php echo $row['post_id'];?>')"><img name = "heart<?php echo $row['post_id'];?>" src = "/images/heart.png" class = "thumbnail" width = "15" /></a> 
      <p class = "title"><img class = "favicon" width = "16" height = "16" src = "<? echo $row['favicon']; ?>" /><a href = "<? echo $row['post_url']; ?>" target = "_blank"><? echo $row['post_title']; ?></a></p> 
      <p class = "postinfo">posted <? echo doRelativeDate($row['date']); ?> by <a href = "<? echo $row['blog_url'];?>"><? echo $row['blog_name']; ?></a> 
     </li> 

    <? 
     } 
    ?> 
+0

感謝您的回覆。我想我應該已經更清楚了:我需要幫助編寫AJAX,這將允許我投票。 – Shola 2010-08-30 23:42:19

回答

0

「SRC = 」/images/heart.png「 類= 」縮略圖「 寬度= 」15「 id="voteImage />

一個ID添加到您的圖像。趕上任何JavaScript框架點擊這個標識事件。

我給jQuery的例子。

jQuery("#voteImage").live("click",function(){ 
     var imageName = jQuery(this).attr('name'); 
     var postId = imageName.substr(5); //Here you will have post Id because remove heart from heart20 

     //now you can hit ajax call to your vote-up or vote-Down php with postId 
     jQuery.ajax({ 
      type: 'POST', 
      url: baseURI+'voteup.php', 
      data:"postId="+postId, 
      cache: false, 
      success: function(result) 
        { 
        //perform further action like give alert to user that action performed 
        } 
     }); 

}