所以我要使用AJAX和PHP提取一些值,然后显示它们。这只是一个投票系统。我想知道的是如何正确显示结果,以便在客户端更新值。这是代码示例及其下面的功能。
这里是AJAX部分,可从json中的PHP文件获取数组响应。
function upVoteIncrementValue(postID){
event.preventDefault();
//var upVoteIncrement = $("#upVoteIncrement").val();
$.ajax({
type: "POST",
url: "voting.php",
data: {
"upVoteIncrement": postID,
},
dataType: "json",
cache: false,
success: function(response){
if(response){
var response = $.trim(response);
if(response){
//$('#voteCount').html(response);
$('#voteCount-' + postID).html(response); //This is the working update
}
else {
return false;
}
}
}
});
}
然后是显示结果的代码:
public function forumview($query){
$stmt = $this->db->prepare($query);
$stmt->execute();
$results = $stmt->fetchAll();
if($stmt->rowCount()>0){
foreach($results as $row){
echo '<tr>';
echo '<td style="color: #333;"><span class="pull-right">';
if(isset($_SESSION['user_session'])){
echo '<a href="#" class="upVoteArrow" onclick="upVoteIncrementValue('.$row['topic_id'].');"><i class="fa fa-arrow-up"></i></a> ';
}else{
echo '<a href="#" id="loginForm" class="upVoteArrow" data-toggle="modal" data-target="#loginModal"><i class="fa fa-arrow-up"></i></a> ';
}
//This is the part that displays the result but instead of
//displaying each in its respective div it only displays in the top div
echo '<span id="voteCount-'.$row['topic_id'].'">'.$this->cleanNumber($row['topic_likes']).'</span>';
if(isset($_SESSION['user_session'])){
echo ' <a href="#" class="downVoteArrow" onclick="downVoteDecrementValue('.$row['topic_id'].');"><i class="fa fa-arrow-down"></i></a>';
}else{
echo ' <a href="#" id="loginForm" class="downVoteArrow" data-toggle="modal" data-target="#loginModal"><i class="fa fa-arrow-down"></i></a>';
}
这里是它在做什么的图片
它递增...该部分在第一个和第二个示例中工作正常,但是当我向上投票下方的下一个值时,它将在第一个位置显示该值(98向上移动到第一个位置,并递增到99)我怎样才能使98递增而不显示在第一个位置?再次感谢您,并感谢所有反馈。
如果对任何想使用它的人都很方便,这是PHP数据库部分:
if(isset($_POST['upVoteIncrement'])){
$upVoteIncrement = $_POST['upVoteIncrement'];
$stmt = $conn->prepare('UPDATE topics SET topic_likes = topic_likes+1 WHERE topic_id = :id LIMIT 1');
$stmt->bindParam(':id', $upVoteIncrement);
$stmt->execute();
$upVote = $conn->prepare('SELECT topic_likes FROM topics WHERE topic_id = :id');
$upVote->bindParam(':id', $upVoteIncrement);
$upVote->execute();
$upVoteCount = $upVote->fetchAll();
if($upVote->rowCount() > 0){
foreach($upVoteCount as $row){
$up = $row['topic_likes'];
$results[] = $up;
}
}
echo json_encode($results);
}
if(isset($_POST['downVoteDecrement'])){
$downVoteDecrement = $_POST['downVoteDecrement'];
$stmt = $conn->prepare('UPDATE topics SET topic_likes = topic_likes-1 WHERE topic_id = :id LIMIT 1');
$stmt->bindParam(':id', $downVoteDecrement);
$stmt->execute();
$downVote = $conn->prepare('SELECT topic_likes FROM topics WHERE topic_id = :id LIMIT 1');
$downVote->bindParam(':id', $downVoteDecrement);
$downVote->execute();
$downVoteCount = $downVote->fetchAll();
if($downVote->rowCount() > 0){
foreach($downVoteCount as $row){
$down = $row['topic_likes'];
$results[] = $down;
}
}
echo json_encode($results);
}