Php json_编码在while循环中不工作
我的php脚本中有一个json_编码,如果在while循环中调用json_编码,那么Jquery端似乎会失败 当我在浏览器窗口(而不是我需要的页面)中查看结果时,我可以看到它确实有效 例如:Php json_编码在while循环中不工作,php,jquery,Php,Jquery,我的php脚本中有一个json_编码,如果在while循环中调用json_编码,那么Jquery端似乎会失败 当我在浏览器窗口(而不是我需要的页面)中查看结果时,我可以看到它确实有效 例如: while ($row = mysql_fetch_array($result)) { $id = $row['id']; $title = $row['title']; $content = $row['content']; $data = array("id"
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}
浏览器中的输出为:
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\u encode($data)代码>就在while循环之外,jQuery拾取响应,但是只有一条记录——这是循环中的最后一条记录
$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);
我需要jQuery从循环中提取所有结果
$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);
这是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>");
}
}
});
$.ajax({//将其发送以进行处理
类型:“POST”,
数据类型:“json”,
url:“includes/auto_suggest.php”,
数据:{
q:输入字符串
},
成功:功能(结果){
如果(result.success){
var id=result.id;
var title=result.title;
$('#auto_id')。追加(“”);
$('auto#u title')。前缀(“”+title+””;
}
}
});
有什么想法吗
提前感谢好吧,您没有发送有效的JSON。
将对象存储在一个数组中,然后在循环后对该数组进行json\u编码
$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代码以正确处理数组而不是对象。因为这里没有人知道你到底想做什么,所以你必须自己做,或者提供更多信息。JSON需要全部位于一个数组或对象上。您需要将每一行附加到一个数组中,然后json\u 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>");
}
});
}
});
$.ajax({//将其发送以进行处理
类型:“POST”,
数据类型:“json”,
url:“includes/auto_suggest.php”,
数据:{
q:输入字符串
},
成功:功能(结果){
$.each(结果,函数(){
如果(这是成功){
var id=this.id;
var title=this.title;
$('#auto_id')。追加(“”);
$('auto#u title')。前缀(“”+title+””;
}
});
}
});
也许
$data = array();
while( WHATEVER ){
// other code
$data[] = array('id' => $id, 'title' => $title, 'success' => true);
}
echo json_encode($data);
将所有信息存储在一个数组中,然后对该数组进行编码。您不能将这样的多个对象传递给jQuery。当while循环完成时,放入数组并打印对象数组。您需要在while循环之外对json进行编码。它将返回单个json,这实际上是正确的
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);
您需要将结果数据添加到数组中,并调用json\u 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);
这将更改输出的JSON(因为当前JSON无效),您现在必须在jQuery回调中循环该JSON。当该行在循环中时返回的JSON作为一个整体无效。它是多个JSON字符串的组合。你需要把他们分开
我将创建一个数组,并在循环中向该数组添加每个$data
。然后,在循环之外,json\u对它进行编码并回显。这将创建一个JSON字符串以返回到javascript。您的JSON输出无效。请尝试此完整代码(编辑):
在javascript代码中
$.ajax({ // Send it off for prcessing
type: "POST",
dataType: 'json',
url: "includes/auto_suggest.php",
data: {
q: inputString
},
success: function (results) {
if (results.success) {
for(var i in results.data) {
var result = results.data[i];
var id = result.id;
var title = result.title;
var $newfield = $("<input type='text' class='hideid' id='hideid_" + id + "' value='" + id + "'/>");
$newfield.prepend("<p>" + title + "</p>");
$('#auto_title').append($newfield);
}
}
}
});
$.ajax({//将其发送以进行处理
类型:“POST”,
数据类型:“json”,
url:“includes/auto_suggest.php”,
数据:{
q:输入字符串
},
成功:功能(结果){
如果(结果、成功){
for(results.data中的var i){
var结果=结果。数据[i];
var id=result.id;
var title=result.title;
变量$newfield=$(“”);
$newfield.prepend(“”+title+“”);
$('auto#u title')。追加($newfield);
}
}
}
});
这不是一个json对象。您必须将数据结构更改为一个JSON对象。您可以将它们全部放在一个数组中。感谢所有人的出色响应,非常感谢您的JavaScript无法工作<代码>结果
将是一个数组,而不是一个对象。您需要循环查看结果
。感谢Chumkiu的回复,非常感谢ThiefMaster的回复,非常感谢Vahur的回复,非常感谢Marcus的回复,非常感谢Nickb的回复,非常感谢Moyed的回复,非常感谢Sachlen的回复非常感谢