我有一个“喜欢/不喜欢”按钮和“喜欢”数量字段,它回显一个获得“喜欢”数量的变量,如下所示:
PHP-HTML:
<span>likes: <?php echo $row['likes'];?></span>
<button type="submit" class="btn btn-custom btn-sm liketoggle" name ="like">
//Printing 'Like' if its been liked by user, and 'Unlike' if not liked by user
<? $qid = $row['Question_ID']; $query2 = "SELECT * FROM likes WHERE
user_id='$user_id' and qid = '$qid'";
$results2 = mysqli_query($con,$query2);
if(mysqli_num_rows($results2) == 0){ echo '<span>Like</span>';}
else{echo '<span>Unlike</span>';}?></button>
Javascript-切换喜欢/不喜欢:
$(".liketoggle").click(function () {
$(this).find("span").text(function(i, v){
return v === 'Like' ? 'Unlike' : 'Like'
return v === 'Unlike' ? 'Like' : 'Unlike'
})
});
有没有办法在用户点击喜欢时增加喜欢的数量($ row ['likes']),而在不喜欢时以相同的方式减少喜欢的数量?