我写了一些代码来构建类似的帖子。那是当我单击照片以更改图像,然后在喜欢的数字上添加一个数字,然后再次单击时,返回上一个数字。 我编写的代码对于单击“插入”和“ dilite”查询都适用,并且当我单击照片时,图片会更改并且数字会增加,但是如果再次单击,它将不再起作用,并且我必须刷新页面agane才能正常工作。 index.php:
<?php
$pgt=1;
$uid=1;
$pst="SELECT * FROM `tbl_users_posts` WHERE id='$pgt'";
$rzp=mysqli_query($conn,$pst);
$rpz=mysqli_fetch_assoc($rzp);
$sid=$rpz['id'];$pos=$rpz['post'];
echo $pos;
?>
<span class="ic_lk ic_">
<?php
$iamlkp="SELECT id FROM `t_plik` WHERE pid='$sid'";
$imlkp=mysqli_query($conn,$iamlkp);
$mylkp=mysqli_num_rows($imlkp);
$iamlkp2="SELECT id FROM `t_plik` WHERE pid='$sid' AND uid='$uid'";
$imlkp2=mysqli_query($conn,$iamlkp2);
$mylkp2=mysqli_num_rows($imlkp2);?>
</span>
<script>
$('document').ready(function(){
var mylk2= <?php echo $mylkp2;?>;
var pgt= <?php echo $pgt;?>;
var uid= <?php echo $uid;?>;
if(mylk2==0) {
$('.ic_lk').html('<img class="li_ik1" src="_pc/lk.png"></img>');
}else if(mylk2>0){
$('.ic_lk').html('<img class="li_ik2" src="_pc/lkm.png"></img>');
}
$(".li_ik1").click(function(){
$.ajax({
url: "ins.php" ,
type: 'POST',
data:{pgt:pgt,uid:uid}
});
$(this).replaceWith('<img class="li_ik2" src="_pc/lkm.png"></img>');
$('.nm_lk').replaceWith('<span class="nm_lk nm_"><?php echo $mylkp+1;?></span>');
});
$(".li_ik2").click(function(){
var mylk2= <?php echo $mylkp2;?>;
var pgt= <?php echo $pgt;?>;
var uid= <?php echo $uid;?>;
var ik1=ik1;
$.ajax({
url: "del.php" ,
type: 'POST',
data:{pgt:pgt,uid:uid,ik1:ik1}
});
$(this).replaceWith('<img class="li_ik1" src="_pc/lk.png"></img>');
$('.nm_lk').replaceWith('<span class="nm_lk nm_"><?php echo $mylkp-1;?></span>');
});
});
</script>
ins.php:
$sid=$_POST['pgt'];
$uid=$_POST['uid'];
if(isset($_POST['pgt'])){
$inpp=mysqli_query($conn,"INSERT INTO t_plik (pid,uid)VALUES('$sid','$uid')");
}
del.php:
$sid=$_POST['pgt'];
$uid=$_POST['uid'];
if(isset($_POST['pgt'])){
$inpp=mysqli_query($conn,"DELETE FROM t_plik WHERE pid='$sid' AND uid='$uid'");
}
谢谢
答案 0 :(得分:0)
这是因为您将onClick
事件绑定到了动态创建的span
您应该尝试以下
<?php
$pgt=1;
$uid=1;
$pst="SELECT * FROM `tbl_users_posts` WHERE id='$pgt'";
$rzp=mysqli_query($conn,$pst);
$rpz=mysqli_fetch_assoc($rzp);
$sid=$rpz['id'];$pos=$rpz['post'];
echo $pos;
?>
<div id="like_wrapper">
<span class="ic_lk ic_">
</div>
<?php
$iamlkp="SELECT id FROM `t_plik` WHERE pid='$sid'";
$imlkp=mysqli_query($conn,$iamlkp);
$mylkp=mysqli_num_rows($imlkp);
$iamlkp2="SELECT id FROM `t_plik` WHERE pid='$sid' AND uid='$uid'";
$imlkp2=mysqli_query($conn,$iamlkp2);
$mylkp2=mysqli_num_rows($imlkp2);?>
</span>
<script>
$('document').ready(function(){
var mylk2= <?php echo $mylkp2;?>;
var pgt= <?php echo $pgt;?>;
var uid= <?php echo $uid;?>;
if(mylk2==0) {
$('.ic_lk').html('<img class="li_ik1" src="_pc/lk.png"></img>');
}else if(mylk2>0){
$('.ic_lk').html('<img class="li_ik2" src="_pc/lkm.png"></img>');
}
$("#like_wrapper").on('click','span.li_ik1',function(){
$.ajax({
url: "ins.php" ,
type: 'POST',
data:{pgt:pgt,uid:uid}
});
$(this).replaceWith('<img class="li_ik2" src="_pc/lkm.png"></img>');
$('.nm_lk').replaceWith('<span class="nm_lk nm_"><?php echo $mylkp+1;?></span>');
});
$("#like_wrapper").on('click','img.li_ik2',function(){
var mylk2= <?php echo $mylkp2;?>;
var pgt= <?php echo $pgt;?>;
var uid= <?php echo $uid;?>;
var ik1=ik1;
$.ajax({
url: "del.php" ,
type: 'POST',
data:{pgt:pgt,uid:uid,ik1:ik1}
});
$(this).replaceWith('<img class="li_ik1" src="_pc/lk.png"></img>');
$('.nm_lk').replaceWith('<span class="nm_lk nm_"><?php echo $mylkp-1;?></span>');
});
});
</script>
尝试一下,然后回发,我将尝试遵循此主题。