PHP变量无法使用json_encode()正确返回成功的AJAX/jQuery POST [英] PHP variables not returning to success AJAX/jQuery POST correctly using json_encode()
问题描述
我已经尝试了几个小时才能使它正常工作.我有一个<div id=""data_friends>
标记和一个hidden input field
我想使用AJAX更新. div
标签如下:
I've been trying for hours to get this to work. I have a <div id=""data_friends>
tag and a hidden input field
that I want to update using AJAX. The div
tag is as follows:
<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
:
I'm clicking on a View More Friends button and sending data to includes/handlers/ajax_load_profile_friends.php
using the following:
$.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的数据如下:
The data coming from ajax_load_profile_friends.php looks like this:
$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条记录的返回值
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);
隐藏输入字段<input id='max' type='hidden' value='<?php echo $num_rows; ?>'>
中的值未使用此$('#max').val(data.num_rows);
The value in my hidden input field <input id='max' type='hidden' value='<?php echo $num_rows; ?>'>
is not updating using this $('#max').val(data.num_rows);
ajax_load_profile_friends.php
中是否缺少引起这些行为的某些东西?
Is there something I'm missing in ajax_load_profile_friends.php
that is causing these behaviors?
**请记住,当我不使用json_encode
& ;;编写类似$('#data_friends').html(data.html);
这样的成功函数,并从AJAX中删除dataType: 'json',
.这里的问题在于,无论哪种方式,我都无法更新隐藏的输入值.我想我会尝试并纠正此方法,因为大多数示例都将json_encode()
指定为返回数据的方式.
**Keep in mind that I can get this to work when I don't use json_encode
& write success function like this $('#data_friends').html(data.html);
and remove the dataType: 'json',
from AJAX. The problem here is that in both ways, I'm not able to update my hidden input value. I figured I would try and correct this method since most examples specify json_encode()
as the way to return data.
推荐答案
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));
这篇关于PHP变量无法使用json_encode()正确返回成功的AJAX/jQuery POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!