2010-08-30 183 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 
        } 
     }); 

}