2016-07-23 118 views
-1

我正在使用json作为按钮。当用户点击它时,正确地增加了类似的计数,并在数据库中存储了相应的id,但是它显示了浏览器中所有id的增量是错误的。我想显示只有用户喜欢的ID,但它显示所有。json递增一个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

+0

请清理你的代码,单独发布javascript并发布分别处理jquery帖子的php。 – e4c5

回答

0

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

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

对于每个帖子都不一样。请检查 – tumbin

+1

是否可以删除你的答案 – tumbin

+0

不,他们都一样。这是截图。 http://i.hizliresim.com/mLnLrR.png –

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> 
+0

感谢您的回复。请在我现在的代码中提供帮助。 ???? – tumbin

0

而不是使用每个条目的隐藏输入,你可以使用按钮本身的数据集与相应的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价值的地方 - 这是展示你怎么能离开只是示例代码隐藏字段和重复ID的问题。

+0

可以请你在我的代码中提供帮助。我会感激 – tumbin