php - json_encode 在 while 循环内不起作用

标签 php jquery

我的 php 脚本中有一个 json_encode,如果在 while 循环内调用 json_encode,它似乎会使 Jquery 方面失败。

当我在浏览器窗口而不是我需要结果的页面中查看结果时,我可以看到它确实有效。

例如:

while ($row = mysql_fetch_array($result)) {     
    $id = $row['id'];
    $title = $row['title'];
    $content = $row['content'];

    $data = array("id" => $id,
    "title" => $title,
    "success" => true
    );
echo json_encode($data); // Inside the While Loop

}

浏览器中的输出是:

{"id":"15","title":"Advanced Booking Examples","success":true}
{"id":"14","title":"Advanced Booking Fields","success":true}
{"id":"11","title":"Add New Driver","success":true}
{"id":"10","title":"Registering a Driver \/ Add New Vehicle","success":true}
{"id":"8","title":"Dispatch Controls","success":true}
{"id":"7","title":"Troubleshooting Driver Device Checklist","success":true}
{"id":"6","title":"Requirements Checklist","success":true}

在 jQuery 响应中,这实际上是空白的,但如果我将 echo json_encode($data); 留在 while 循环之外,jQuery 会拾取响应,但只有 1 条记录 - 是最后一条记录在循环中记录。

我需要 jQuery 来获取循环中的所有结果。

这是 JS

$.ajax({ // Send it off for prcessing
        type: "POST",
        dataType: 'json',
        url: "includes/auto_suggest.php",
        data: {
            q: inputString
        },
        success: function (result) {
            if (result.success) {             
                var id = result.id;
                var title = result.title;                    
                $('#auto_id').append("<input type='text' class='hideid' id='hideid_" + id + "' value='" + id + "'/>");              
        $('#auto_title').prepend("<p>" + title + "</p>");
            }
        }
    });

有什么想法吗?

提前致谢

最佳答案

JSON 需要全部位于一个数组或对象上。您需要将每一行附加到一个数组,然后 json_encode

$data = array();
while ($row = mysql_fetch_array($result)) {     
    $id = $row['id'];
    $title = $row['title'];
    $content = $row['content'];

    $data[] = array(
      "id" => $id,
      "title" => $title,
      "success" => true
    );
}
echo json_encode($data);

然后在 JavaScript 中,您将拥有一个可以循环的对象数组。

$.ajax({ // Send it off for prcessing
    type: "POST",
    dataType: 'json',
    url: "includes/auto_suggest.php",
    data: {
        q: inputString
    },
    success: function (result) {
        $.each(result, function(){
          if (this.success) {
            var id = this.id;
            var title = this.title;
            $('#auto_id').append("<input type='text' class='hideid' id='hideid_" + id + "' value='" + id + "'/>");
            $('#auto_title').prepend("<p>" + title + "</p>");
         }
        });
    }
});

关于php - json_encode 在 while 循环内不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10937699/

相关文章:

jquery - 如何在 jquery html() 函数中打印多个 html 元素?

jquery - yii2 : kartik-v/fileinput trigger upload on selection

php - 刚刚完成了图像上传 php mysql

php - 在我们使用 AWS SES SMTP 凭证在 PHP 应用程序中发送电子邮件的 AWS SES 中迁移到签名版本 4 的影响?

javascript - 在拖放时获取 jstree 的元素 ID

jquery - 使用ajax json加载数据后Bootstrap数据表不起作用

jquery - 如何防止移动端界面头部与内容重叠,同时保证内容始终保持在界面下方?

php - 使用准备好的语句提取 MySQL 信息时使用 AES 进行解密

php - 非常简单的 RewriteRule 不起作用

php - 保护从 ajax 调用的我的 php 脚本的执行