【问题标题】:json increments one id but displays all idsjson 增加一个 id 但显示所有 id
【发布时间】:2016-07-23 13:47:00
【问题描述】:

我正在使用 json 作为like 按钮。当用户正确点击时,它会增加点赞数并存储在数据库中以获取相应的 ID,但它会显示浏览器中所有 ID 的增量,这是错误的。我只想显示用户喜欢的 id,但它会显示给所有人。

测试地址为http://way2enjoy.com/app/check/test/indexp.php

indexp.php 文件是

<?php


include('/home/xxx/con.php');


   $query="Select * from users_jokes order by id desc limit 10";
   $result=mysql_query($query);


?>

<html>
<head>
<meta charset="utf-8">

<script src="jquery-3.1.0.min.js"></script>
<script type="text/javascript">
            var ajaxSubmit = function(formEl) {

                var url = $(formEl).attr('action');


                var comment=document.getElementById("jokes_comment").value;
                var joke_id=document.getElementById("joke_id_hidden").value;
                $.ajax({
                    url: url,
                    data:{
                        'action':'addComment',
                        'comment':comment,
                        'joke_id':joke_id
                    },
                    dataType: 'json',
                    type:'POST',
                    success: function(result) {

                            console.log(result);
                            $.ajax({
                            url: url,
                            data:{
                            'action':'getLastComment',
                            'joke_id':joke_id
                            },
                            dataType: 'json',
                            type:'POST',
                            success: function(result) {                             
                            $('#jokes_comment').val("");
                            console.log(result[0].description);
                            $("#header ul").append('<li>'+result[0].description+'</li>');                           

                            },
                            error: function(){
                            alert('failure');


                    }
                });

                    },
                    error: function(){
                        alert('failure');


                    }
                });

                // return false so the form does not actually
                // submit to the page
                return false;
            }

        var ajaxLike=function()
        {
            var joke_id=document.getElementById("joke_id_hidden").value;
            // setup the ajax request
            $.ajax(
            {
                url: 'likeskk.php',
                data:{
                    'action':'increment_like',
                    'joke_id':joke_id
                    },
                dataType: 'json',
                type:'POST',
                success: function(result) 
                {

                        $.ajax(
                    {

                        url: 'likeskk.php',
                        data:{
                            'action':'display_like',
                            'joke_id':joke_id
                            },
                        dataType: 'json',
                        type:'POST',
                        success: function(result)
                        {

                            console.log(result);
                                               $("label[for='like_counter']").text(result.likes);


                        },
                        error: function(result)
                        {
                            alert("error 2");

                        },

                    });
                },
                error: function()
                {
                    alert('failure');

                }
            });

           return false;
        }
        </script>

<p>commnet list</p>
<div id="header">
<ul id="commentlist" class="justList">
<?php
 $query="Select * from comment where joke_id='2'";
   $result=mysql_query($query);

   while($data = mysql_fetch_array($result)){

    $cont = $data['description'];
    ?>
    <li><?php echo $cont;
    ?></li>
    <?php
   }
?>
</ul>
</div>
<?php

?>
<form  method="post" action="processkk.php" onSubmit="return ajaxSubmit(this);">
<input type=text id="jokes_comment" name="jokes_comment">

</input>
<input type="submit" value="comment">

</form>
</body>
</html>


<?php 
while($data = mysql_fetch_array($result)){
    $id = $data['id'];
    $cont = $data['content'];
    $likes = $data['likes'];
    ?>
    <p><?php echo $cont;?></p>
    <input type="hidden" value="<?php echo $id ;?>" id="joke_id_hidden">
    <p><button  onClick="ajaxLike();">Like</button> <label for="like_counter"><?php echo $likes;?></label></p>
    <?php }
?>

</body>
</html>



likeskk.php 

    <?php


     include('/home/xxxxxxx/con.php');



         $action=$_POST['action'];
         if($action=="increment_like")
         {
             $joke_id=$_POST['joke_id'];
            $query="update users_jokes set likes =likes+1 where id='".$joke_id."'";
            $result=mysql_query($query);

            // setup our response "object"

            $retVal=array("Success"=>"true");
             print json_encode($retVal);

         }
          if($action=="display_like")
         {
            $joke_id=$_POST['joke_id'];
            $query = "select likes from users_jokes where id = '$joke_id'";
            $qry = mysql_query($query);
            while($rows = mysql_fetch_array($qry)){
                $likes = $rows['likes'];
            }

            header('Content-Type: application/json');
            // print json_encode(array('foo' => 'bar'));
            print json_encode(array('success'=>'true','likes'=>$likes));


         }

    ?>

当我点击一个喜欢所有喜欢的增加。当我对一个 id 发表评论时,它会附加并显示在所有 id 中

【问题讨论】:

  • 请清理您的代码,单独发布javascript并单独发布处理jquery帖子的php。

标签: javascript php jquery mysql json


【解决方案1】:

复制粘贴。

<html>
<head>

<script>
    function clickCounter(element)
    {
        element.value = parseInt(element.value) + 1;
    }
</script>

</head>
<body>
    <input type="button" value="0" onclick="clickCounter(this)"/>
    <input type="button" value="0" onclick="clickCounter(this)"/>
    <input type="button" value="0" onclick="clickCounter(this)"/>
    <input type="button" value="0" onclick="clickCounter(this)"/>
    <input type="button" value="0" onclick="clickCounter(this)"/>
    <input type="button" value="0" onclick="clickCounter(this)"/>
    <input type="button" value="0" onclick="clickCounter(this)"/>
</body>
</html>

【讨论】:

    【解决方案2】:

    因为您使用的是相同的 id。 id 必须是唯一的

    <input type="hidden" value="4638" id="joke_id_hidden">
    

    【讨论】:

    • 每篇文章都不同。请检查
    • 你可以删除你的答案
    • 不,它们都是一样的。这是屏幕截图。 i.hizliresim.com/mLnLrR.png
    • 我会尽力帮助你。在您的 html 代码中搜索 joke_id_hidden。您将在 10 条不同的行中看到
    • 在此链接中我更改了 ID way2enjoy.com/app/check/test/indexp1.php,但随后如何更改 javascript。对于每一行我不能有完整的 javascript var joke_id=document.getElementById("joke_id_hidden").value;
    【解决方案3】:

    您可以在按钮本身上使用具有相应 ID 的数据集,而不是为每个条目使用隐藏输入。单击按钮时,javscript 函数可以读取该数据集值并在 ajax POST 请求中使用该值。所以,举个例子:

    <!doctype html>
    <html>
        <head>
            <title>Like the jokes...</title>
        </head>
        <body>
    
            <form id='bttns'>
                <?php
                    /* pseudo generate some jokes with buttons and labels to emulate your page */
                    for( $i=1; $i < 20; $i++ ){
    
                        $random=rand(1,20);
    
                        echo "
                        <p>Funny joke...[{$i}]</p>
                        <input type='button' value='Like' data-id='$i' />
                        <label class='counter'>{$random}</label>";
                    }
                ?>
            </form>
    
            <script type='text/javascript'>
                var col=document.querySelectorAll('#bttns input[type=\"button\"]');
    
                for( var n in col )if(col[n].nodeType==1)col[n].onclick=function(e){
    
                    var el=typeof(e.target)!='undefined' ? e.target : e.srcElement;
                    var label=el.nextSibling.nextSibling;
                    var id=el.dataset.id;
    
                    label.innerHTML=parseInt( label.innerHTML )+1;
    
                    alert( 'use ajax to POST this id: '+id );
                }
            </script>
        </body>
    </html>
    

    也许我应该补充一点,ajax 回调函数将是为每个笑话更新 likes 值的地方,而不是像这里那样 - 它只是示例代码,展示了如何消除隐藏字段和重复问题身份证。

    【讨论】:

      猜你喜欢
      • 2022-11-18
      • 1970-01-01
      • 1970-01-01
      • 2021-08-25
      • 2016-10-11
      • 2015-03-19
      • 2018-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多