我正在用AJAX和PHP开发一个投票系统,我遇到了一些麻烦。我们显示了数据库中的一组帖子,每个帖子旁边都有一张图片--单击图片的目的是1)切换图片颜色,然后2)使用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>
<?
}
?>发布于 2010-08-31 14:06:39
“src =”/ /> /Heart.png“class =”缩略图“width = "15”id="voteImage图像
为您的镜像添加一个ID。任何javascript框架都可以在此Id上捕获单击事件。
我给出了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
}
});
}https://stackoverflow.com/questions/3604953
复制相似问题