PHP 变量无法使用 json_encode() 正确返回成功 AJAX/jQuery POST

2024-05-05

我已经尝试了几个小时才能让它发挥作用。我有一个<div id=""data_friends>标签和一个hidden input field我想使用 AJAX 进行更新。这div标签如下:

<div class="friends-tab-list" id="data_friends">

    <?php 
    //Default query limits results to 8

    $sql = ("SELECT * FROM users WHERE FIND_IN_SET(?, friend_array) > 0 LIMIT 0,8");
    $query = mysqli_prepare($con, $sql);
    $query->bind_param('s', $username);
    $query->execute();

    $result = $query->get_result();

    $num_rows = $result->num_rows;

        while ($row = $result->fetch_assoc()) {
        $row['profile_pic']; 
        $row['username'];

        echo "<a class='profile-img-item' href='" . $row['username'] . "'>
              <img src='" . $row['profile_pic'] . "' title='" . $row['username'] . "'>
              </a>";
                }
        $query->close();

          ?>
</div>

隐藏输入如下:<input id='max' type='hidden' value='<?php echo $num_rows; ?>'>

我点击“查看更多好友”按钮并将数据发送至includes/handlers/ajax_load_profile_friends.php使用以下内容:

$.ajax({

    url:'includes/handlers/ajax_load_profile_friends.php',
    type:'POST',
    dataType: 'json',
    data:{'username':username, 'num_friends':num_friends, 'counter':counter},

        success: function(data) {
            $('#data_friends').html(data.html);
            $('#max').val(data.num_rows);
                }
        });

来自 ajax_load_profile_friends.php 的数据如下所示:

$query = mysqli_prepare($con,"SELECT * FROM users WHERE FIND_IN_SET(?, friend_array) LIMIT $counter"); 
$query->bind_param('s', $username);
$query->execute();

$result = $query->get_result();

$num_rows = $result->num_rows;
}       

while ($row = $result->fetch_assoc()) {
$row['profile_pic']; 
$row['username'];

$html = "<a class='profile-img-item' href='" . $row['username'] . "'>
          <img src='" . $row['profile_pic'] . "' title='" . $row['username'] . "'>
         </a>";

}   

echo json_encode(array('num_rows' => $num_rows, 'html' => $html));

When I run this, I get a single return in my when I'm suppose to get a return of 16 records with each click I thought by doing this in my success function $('#data_friends').html(data.html);
View all friends button

我的隐藏输入字段中的值<input id='max' type='hidden' value='<?php echo $num_rows; ?>'>不使用此更新$('#max').val(data.num_rows);

有什么我遗漏的吗ajax_load_profile_friends.php是什么导致了这些行为?

**请记住,当我不使用时我可以让它工作json_encode& 像这样编写成功函数$('#data_friends').html(data.html);并删除dataType: 'json',来自阿贾克斯。这里的问题是,在这两种方式中,我都无法更新我的隐藏输入值。我想我会尝试并纠正这个方法,因为大多数示例都指定json_encode()作为返回数据的方式。


header( "Content-Type: application/json", TRUE );
$query = mysqli_prepare($con,"SELECT * FROM users WHERE FIND_IN_SET(?, friend_array) LIMIT $counter"); 
$query->bind_param('s', $username);
$query->execute();

$result = $query->get_result();

$num_rows = $result->num_rows;

$html='';

while ($row = $result->fetch_assoc()) {


$html .= "<a class='profile-img-item' href='" . $row['username'] . "'>
          <img src='" . $row['profile_pic'] . "' title='" . $row['username'] . "'>
         </a>";

}   

echo json_encode(array('num_rows' => $num_rows, 'html' => $html));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

PHP 变量无法使用 json_encode() 正确返回成功 AJAX/jQuery POST 的相关文章

随机推荐