我正在建立一个消息系统,我必须获取消息,这是我的代码
我的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行并显示警报,请任何帮助将不胜感激,也请尝试给出跨浏览器的代码谢谢....
答案 0 :(得分: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>');
}
}
});
}
答案 1 :(得分:0)
你能得到所有结果:
console.log(r);
你可以尝试
$.each(r, function(k, row) {
alert(row.nno);
alert(row.mmessage_from);
}
答案 2 :(得分:0)
试试这个,
function fetch_conversation()
{
$.ajax({
type: 'post',
dataType: 'json',
url: 'message_function.php',
data: {
fetch_conversation:'conversation'
},
success: function (response) {
var msg_no=response['no'];
var msg_from=response['message_from'];
alert(msg_no);
alert(msg_from);
}
});
}
和你的php
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'];
$mmessage_from[]=$row['message_from'];
}
$arr['no']=$nno;
$arr['message_from']=$mmessage_from;
echo json_encode($arr);
exit();
}
答案 3 :(得分:0)
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());
// create a result array to send response
$result = array();
while($row=mysql_fetch_array($select_message))
{
$_temp = array();
$_temp['no'] = $row['no'];
$_temp['message_from'] = $row['message_from'];
$result[] = $_temp;
unset($_temp);
}
@header("Content-Type:text/json");
echo json_encode($result);
exit;
}
请尝试使用上面的代码。这将为您提供由多个数组记录创建的json。您的html代码请按@Ferret建议。