dtfo55908 2019-05-11 17:01
浏览 79
已采纳

需要帮助以正确的方式编写代码来创建“loadmore”按钮和“喜欢”按钮的帖子

I create a load more button for load more posts from the database but when I add like button for that if one time clicks on load more button and then click on the like button, like.php file runs two times and adds two lines in likes table. if I click 2 times on load more then like.php file runs 3 times and... I want to know how I should create a loadmore button and like the button to works fine.

this is simple of my codes:

posts.php :

<div id="comnts2"></div>
<button id="btn2" >load more</button><script>

$(document).ready(function() {

  var comco2 = 2;
  var offset2 = 0;
  $("#btn2").click(function() {
    $.ajax({
        method: "POST",
       url: "ld_comco.php",
        data: { comnco2 : comco2, offset2 : offset2}
      })
      .done(function(msg2) {

          $("#btn2").hide();
        } else {
       $("#comnts2").append(msg2);

      });

    offset2 = offset2 + comco2;
  });
  $("#btn2").trigger("click");
});
</script>

ld_comco.php:

<?php
$comnco2=$_POST['comnco2'];
$offset2=$_POST['offset2'];

$rzp=mysqli_query($conn,"SELECT * FROM `tbl_users_posts` WHERE uid = '$uid' ORDER BY id DESC limit $offset2, $comnco2");

  while($rp=mysqli_fetch_assoc($rzp)){
    $sid=$rz['id'];
    $lik=$rz['lik'];
    echo $sid."<br>";

    /*like*/
    echo'<img class="li_ik1" data-id="'.$sid.'" src="pc3/up.png">'.$lik.' Likes</img>';
?>
    </span>
<?php }?>

<script type="text/javascript">

$(document).ready(function() {
  var uid=<?php echo $uid;?>;

  $(document).on("click", ".li_ik1", function() {
    var psid = $(this).data('id');
      $.ajax({
          method: "POST",
          url: "like.php",
          data: {psid: psid, uid: uid}
        }).done();
  }); 
});
</script>

like.php:

<?php
$id=$_POST['psid'];
$uid=$_POST['uid'];

$Y=mysqli_query($conn,"INSERT INTO `t_plik` (pid,uid) VALUES ('$id','$uid')");
$Q=mysqli_query($conn,"UPDATE `tbl_users_posts` SET lik=lik+1 WHERE id='$id'");
?>

thanks

  • 写回答

2条回答 默认 最新

  • dongzhanjuan5141 2019-05-11 23:07
    关注

    I think the problem is, that you bind your like button multiple times globally. Each time you load the content from ld_comco.php you also call $(document).on("click", ".li_ik1", ...) in the $(document).ready block, which means you bind all ".li_ik1" buttons on the entire document (but some of them has already been bind).

    I would remove the $(document).ready(...) block from the ld_comco.php and move the logic into the posts.php right before you render your content. A further positive aspect is you have your business logic at one place.

    KEEP IN MIND: You get a response of buttons in msg2, thats why you do not need to filter $msg2 anymore. But if you wrap your buttons with further html tags in ld_comco.php, your buttons will be on a deeper level, so you need to use a selector again, like you did with .on("click", ".li_ik1", ...).

    posts.php

    ...
    var $msg2 = $(msg2);
    
    // Now you bind only the loaded buttons instead of 
    // the buttons in the entire document for multiple times
    $msg2.on("click", function() {
      var $element = $(this);
      var psid = $element.data('id');
      var uid = $element.data('uid');
    
      $.ajax({
        method: "POST",
        url: "like.php",
        data: {psid: psid, uid: uid}
      }).done();
    });
    
    $("#comnts2").append($msg2);
    ...
    

    In your ld_comco.php you need to add the data-uid="'.$uid.'" and remove the script block. Then your file should look like this:

    <?php
    $comnco2=$_POST['comnco2'];
    $offset2=$_POST['offset2'];
    
    $rzp=mysqli_query($conn,"SELECT * FROM `tbl_users_posts` WHERE uid = '$uid' ORDER BY id DESC limit $offset2, $comnco2");
    
    while($rp=mysqli_fetch_assoc($rzp)){
      $sid=$rz['id'];
      $lik=$rz['lik'];
      echo $sid."<br>";
    
      /*like*/
      echo'<img class="li_ik1" data-id="'.$sid.'" data-uid="'.$uid.'" src="pc3/up.png">'.$lik.' Likes</img>';
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥20 sim800c模块 at指令及平台
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计