php - 如何从 ajax 调用中获取数组以使用 php 显示正确的结果
问题描述
所以我使用 AJAX 和 PHP 提取一些值,然后显示它们。这只是一个投票系统。我想知道的是如何正确显示结果,以便客户端更新值。这是代码示例及其在下面的作用。
这是从 json.php 文件中获取数组响应的 AJAX 部分。
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);
}
解决方案
推荐阅读
- python-3.x - 为什么 pd.read_sql_query 真的很慢?
- selenium - 无法单击硒中的提交按钮
- android - 单击 fcm 通知以转到具有意图数据的特定活动
- python - Python:确保只从 lambda 中获取一个元素
- operating-system - “流程的执行”究竟是什么时候开始的?它在 cpu / 内存中处于什么阶段?
- dialogflow-es - 如何在不选择任何产品的情况下在谷歌助手中显示产品列表?
- html - IIS 上托管的视频无法在 Web 浏览器中播放
- mysql - 有数量范围的计数和总和
- django - 遵循Mozilla django教程时出现异常“unhashable type:'list'”
- r - 识别和总结 R 中的离散节点组