我正在制作一个消息系统,我必须获取消息,这是我的代码
我的ajax代码来获取结果-
function fetch_conversation() {
$.ajax({
type: 'post',
url: 'message_function.php',
data: {
fetch_conversation:'conversation'
},
success: function (response) {
var r = $.parseJSON(response);
alert(r.nno);
alert(r.mmessage_from);
}
});
}
这是我的 php 代码,用于获取数据并将其作为响应发送到 ajax-
if(isset($_POST['fetch_conversation'])) {
$select_message=mysql_query("select no,message_from,sent_time,message_body from messages where message_to='1' and sent_time > '2014-12-02 17:43:35' union select no,message_to,sent_time,message_body from messages where message_from='1' and sent_time > '2014-12-02 17:43:35' ")or die(mysql_error());
while($row=mysql_fetch_array($select_message)) {
$nno= $row['no'];
echo json_encode($nno);
$mmessage_from=$row['message_from'];
echo json_encode($mmessage_from);
}
exit();
}
我使用的查询获取超过 1 行,我的问题是 json 无法在获取对话函数中返回值,如果查询仅获取 1 行,则 json 显示所有值并且所有警报都是可见的。如何使我的获取对话功能可以获取超过 1 行并显示警报,请提供任何帮助,我们将不胜感激,也请尝试提供跨浏览器的代码,谢谢......
最佳答案
**in the mesage_function.php file**
if(isset($_POST['fetch_conversation'])) {
$select_message=mysql_query("select no,message_from,sent_time,message_body from messages where message_to='1' and sent_time > '2014-12-02 17:43:35' union select no,message_to,sent_time,message_body from messages where message_from='1' and sent_time > '2014-12-02 17:43:35' ")or die(mysql_error());
$array = array();
$i = 0;
while($row=mysql_fetch_array($select_message)) {
$array[$i]['no']= $row['no'];
$array[$i]['mmessage_from']=$row['message_from'];
$i++;
}
echo json_encode($array);
}
**and in the ajax sucess alert only response and check log**
**try this, i hope this help.**
在您的成功响应中使用此代码.. 将此代码放在 parsejson 之后 在这里添加您的表 ID。
#add_your_id_here
function fetch_conversation()
{
$.ajax({
type: 'post',
url: 'message_function.php',
data: {
fetch_conversation:'conversation'
},
success: function (response) {
var r = JSON.parse(response);
var text = "";
var x;
for (x in r) {
$('#add_your_id_here').append('<tr><td>'+r[x]['mmessage_from']+'</td><td>'+r[x]['no']+'</td></tr>');
}
}
});
}
关于php - 如何通过带有json的ajax响应获取超过1个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27313013/