php - 如何通过带有json的ajax响应获取超过1个结果

标签 php jquery mysql ajax json

我正在制作一个消息系统,我必须获取消息,这是我的代码

我的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/

相关文章:

mysql - 将单个事件距离表转换为事件汇总表

mysql - SSIS - 使用 ADO.NET 连接将值插入变量

mysql - 计算研究领域的百分比

php - Laravel 4 中的列歧义错误

javascript - 如何使用输入数组序列化表单

javascript - 当用户单击离开此页面时重定向到页面

jquery - typescript 模型到 json - 忽略空值属性

php - WordPress:如何在任何管理页面上显示元数据框?

php - CakePHP 响应停止已弃用

php - Google Map API 3 标记未显示