如何在php循环中正确运行jquery代码

时间:2018-12-26 19:52:16

标签: php jquery mysql

我有一个代码示例喜欢这些帖子 我把正确的图片与jQuery代码,但当我按loadmore按钮显示所有图片相同! .li_ik1或.li_ik2

http://s8.picofile.com/file/8346985692/as.png

另一个问题是,当我单击图像时,查询行会添加到mysql中,但是在其他任何类似的单击上,都将多次导入与第一个相同的内容。

这是a1.php:

<div  id="comnts2"></div>
<button id="btn2" style="width: 300px;height: 40px;position: relative;left:80px;background-color:aliceblue;font-size: 18px;border: solid  1px #9EBDFC;border-radius: 20px;top: 20px">load more</button>


<script>
$(document).ready(function() {

  var comco2 = 2;
  var offset2 = 0;

  $("#btn2").click(function() {
    $.ajax({
        method: "POST",
       url: "a2.php",
        data: { comnco2: comco2, offset2: offset2 }
      })
      .done(function(msg2) {
        $("#comnts2").append(msg2);
      });
    offset2 = offset2 + comco2;
  });

  $("button").trigger("click");
});
</script>

这是a2.php:

$comnco2=$_POST['comnco2'];
$offset2=$_POST['offset2'];
$uid=1;
$zps=mysqli_query($conn,"SELECT * FROM `tbl_users_posts` WHERE uid =1 limit $offset2,$comnco2");while($rzp=mysqli_fetch_assoc($zps)){
    $pid=$rzp['id'];
    $lik=$rzp['lik'];?>

<div><?php echo $pid;?></div>
<?php $sclk=mysqli_query($conn,"SELECT * FROM t_plik WHERE pid='$pid' AND uid='$uid'");
    $nmlk=mysqli_num_rows($sclk);?>
<span class="ic_lk"></span>

<script>
$('document').ready(function(){
    var lik= <?php echo $nmlk;?>;
    var uid= <?php echo $uid;?>;
    var pid= <?php echo $pid;?>;
if(lik==0) {
        $('.ic_lk').html('<img class="li_ik1" src="pc3/up.png"></img>'); 
        }else if(lik>0){
        $('.ic_lk').html('<img class="li_ik2" src="pc3/uup.png"></img>'); 
    }

$(document).on("click", ".li_ik1",function(){
    $(this).replaceWith('<img class="li_ik2" src="pc3/uup.png"/>');
    });
$(document).on("click", ".li_ik2",function(){
    $(this).replaceWith('<img class="li_ik1" src="pc3/up.png"/>');
    });


    $(".li_ik1").click(function() {
    $.ajax({
        method: "POST",
       url: "b1.php",
        data: { uid: uid, pid: pid }
      });
  });
}); 
</script>

<?php }?>

这是b1.php:

$uid=$_POST['uid'];
$pid=$_POST['pid'];
$inlk=mysqli_query($conn,"INSERT INTO t_plik (uid,pid) VALUES ($uid,$pid)");

我该如何解决? 谢谢

0 个答案:

没有答案