1

我正在使用 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>

    <?
        }
    ?>
4

1 回答 1

0

" src = "/images/heart.png" 类 = "缩略图" 宽度 = "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
                    }
        });

 }
于 2010-08-31T06:06:39.163 回答