我已经尝试了几个小时才能使其正常工作。我有一个<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));
运行此命令时,如果我想通过在成功函数$('#data_friends').html(data.html);
中执行此操作,我认为每次点击都会获得16条记录的回报,那么我得到的回报只有一个
我的隐藏输入字段<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);
的成功函数,然后从AJAX中删除dataType: 'json',
。这里的问题在于,无论哪种方式,我都无法更新隐藏的输入值。我认为我会尝试并纠正此方法,因为大多数示例都将json_encode()
指定为返回数据的方法。
答案 0 :(得分:2)
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));
答案 1 :(得分:1)
您没有在while循环之前声明$ html变量。试试这个
<?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;
}
$html = '';
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));