首页 > 解决方案 > 如何从 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); 

}

标签: phpjqueryajax

解决方案


推荐阅读