php - json_encode not working inside while loop -
i have json_encode in php script seems fail jquery side of things if json_encode called inside while loop.
when view result in browser window other page need result in can see work.
for example:
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 while loop }
and output in browser is:
{"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 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}
in jquery response blank if leave echo json_encode($data);
outside while loop jquery picks response, 1 record - being last record in loop.
i need jquery pick results loop.
here's js
$.ajax({ // send off 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>"); } } });
any ideas?
thanks in advance
json needs on 1 array or object. need append each row array, json_encode
that.
$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);
then in javascript, have array of objects, can loop through.
$.ajax({ // send off 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>"); } }); } });
Comments
Post a Comment