gpt4 book ai didi

PHP 喜欢/不喜欢按钮只在第一次工作

转载 作者:行者123 更新时间:2023-11-29 02:52:40 25 4
gpt4 key购买 nike

我正在尝试在我的页面上实现一个喜欢/不喜欢按钮。我设法让按钮工作(点击时它会变成喜欢,反之亦然),它还会在数据库表上创建或删除类似的东西。现在的问题是喜欢的计数器。它仅在我第一次单击按钮时有效,即如果最初有 2 个赞 并且我不喜欢帖子显示 1 个赞,但如果我再次尝试单击在它上面,它一直显示 1 like,我必须重新加载页面才能看到它更改为 2 likes

这是我目前所拥有的:

JQUERY

$(document).on('click', ".miPiace", function() {
trova = '';
commentoORisposta = '';
valCOR = '';
var comOrisp;

try{
trova = $(this).parentsUntil("#fermamiQui");
commentoORisposta = trova.find(".idCommento");
comOrisp = 'commento';
valCOR = commentoORisposta.val();

} catch(err){
trova = $(this).parentsUntil(".infoCommento");
commentoORisposta = trova.find(".idRisposta");
comOrisp = 'risposta';
valCOR = commentoORisposta.val();

}
valCOR = commentoORisposta.val();



if ($(this).hasClass('fa-thumbs-o-up')) {
$(this).removeClass('fa-thumbs-o-up');
$(this).addClass('fa-thumbs-up');

$.get( "lib/ottieniCose.php", { like: "", id: valCOR, comOrisp: comOrisp } )
.done(function( data ) {
trova.find('.numDiLikes').replaceWith('<p>' + data + ' likes</p>');
});


}else if($(this).hasClass('fa-thumbs-up')){
$(this).removeClass('fa-thumbs-up');
$(this).addClass('fa-thumbs-o-up');

$.get( "lib/ottieniCose.php", { remLike: "", id: valCOR, comOrisp: comOrisp } )
.done(function( data ) {
trova.find('.numDiLikes').replaceWith('<p>' + data + ' likes</p>');
});


};
});

PHP

if (isset($_GET['like'])) {

if ($_GET['comOrisp'] == 'commento') {

$commento->set_likes($_GET['id'], true);
return print $commento->get_likes($_GET['id'], true);

} elseif ($_GET['comOrisp'] == 'risposta') {

$commento->set_likes($_GET['id'], false);
return print $commento->get_likes($_GET['id'], false);
}
} elseif (isset($_GET['remLike'])) {

if ($_GET['comOrisp'] == 'commento') {

$commento->remove_likes($_GET['id'], true);
return print $commento->get_likes($_GET['id'], true);

} elseif ($_GET['comOrisp'] == 'risposta') {

$commento->remove_likes($_GET['id'], false);
return print $commento->get_likes($_GET['id'], false);
}
}

其他有 $commenti 类的 PHP 文件

public function get_likes($id, $commento){

$idComm = 0;
$idRisp = 0;

$retVal = ($commento) ? $idComm = $id : $idRisp = $id;

if ($idComm != 0){
$query = "SELECT commento,
(SELECT COUNT(*) FROM likes
WHERE commento = {$idComm})
AS like_count FROM likes";

} elseif($idRisp != 0){
$query = "SELECT risposta,
(SELECT COUNT(*) FROM likes
WHERE risposta = {$idRisp})
AS like_count FROM likes";

}

$trovaQuanti = mysqli_query($_SESSION['connessione'], $query);
$trovaDavveroQuanti = mysqli_fetch_assoc($trovaQuanti);

if ($trovaDavveroQuanti == null) {
return '0';
}

return $trovaDavveroQuanti['like_count'];

}

最佳答案

您可能想尝试正常的 $.ajax 调用。并关闭缓存。有时这会导致您需要刷新才能看到更改的问题。

$.ajax({
type: 'GET',
cache: false,
url: "lib/ottieniCose.php",
data: { like: "", id: valCOR, comOrisp: comOrisp },
dataType: "html",
success: function(html){
trova.find('.numDiLikes').html('<p>' + data + ' likes</p>');
}
});

关于PHP 喜欢/不喜欢按钮只在第一次工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34051940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com