dqyy38265 2013-06-13 09:55
浏览 42

如何使用jquery构建类似的计数器?

I'm looking to make a like counter as like in facebook. I will load various posts from database with message id. i want to make like(here it is "pray") counter and it should do behind the page using jquery and ajax. I cant get specific message id(mid). here the code is

jQuery:

$(document).ready(function () {
    $("div.pray").click( function()
    {
        var mid=$(this).val();
        $.ajax(
        {
            type:"POST",
            url:"increment.php",
            data:'mid='+mid,
            success:function ()
            {
                $("div.pray").text("Prayed");
            }
    });
});

PHP:

    session_start();
    if(isset($_SESSION['usrid']))
    {
        $usr = $_SESSION['usrid'];
        include('dbcon.php');
        $cot = "select * from posts where userid=$usr LIMIT 10";// order by eventDate        DESC, eventHour DESC" LIMIT0,10";
        $ex = mysql_query($cot,$con);

        while($cont=mysql_fetch_array($ex))
        {
            $date = date_create($cont['date']);
            $mid=$cont['mid'];
            echo "<div id='posts'>";
            echo $cont['message'];
            echo $photo;
            echo "<div class='pray'>"; 
            echo "<input type='hidden' class='mid' value='$mid'>"; 
            echo "<a href='#'>Pray</a></div>";
            echo "Prayed(".$cont['prayers'].")&nbsp;&nbsp&nbsp;&nbsp";
            echo date_format($date, 'd-m-Y H:i:s');
            echo "<hr>";
            echo "</div>";
        }
    }
    else
    {
        echo "Login to see";
    }
?>

i want to replace only the message which i clicked as pray and also it should load current number of prayed.

  • 写回答

2条回答 默认 最新

  • duanqin7791 2013-06-13 10:02
    关注

    put your javascript AFTER your php script and check. If that fails, change your .click() handler to,

    $(document).on('click', 'div.pray', function(){
        var mid=$(this).children('.mid').val();
        $.ajax(
        {
             type:"POST",
             url:"increment.php",
             data:'mid='+mid,
             success:function ()
             {
                  $("div.pray").text("Prayed");
             }
        });
    
        //rest of your code
    })
    

    Notice, I've changed the first line from

    var mid=$(this).val();
    

    To

    var mid=$(this).children('.mid').val();
    

    As, $(this) will select the div.pray and not the input.mid element.

    评论

报告相同问题?

悬赏问题

  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目