my problem with json_encode()? [closed]
result why are in json_encode() next to each other?
php:
function search_hotel(){
$search_term = $this->input->post('search_hotel');
$query = $this->db->order_by("id", "desc")->like('name', $search_term)->get('hotel_submits');
$data = array();
foreach ($query->result() as $row)
{
$data[] = $row->name;
}
echo json_encode(array('name' => $data));
// echo: {"name":["333333","\u0633\u0644","\u0633\u0644\u0627\u0633\u06cc","\u0633\u0644\u0627\u0633\u0633","\u0633\u0644\u0627\u0645"]}
}
js:
$('#hotel').keyup(function () {
var dataObj = $(this).closest('form').serialize();
$.ajax({
type: "POST",
url: 'http://localhost/Siran-mehdi/admin/tour/search_hotel',
data: dataObj,
cache: false,
dataType: 'json',
success: function (data) {
$(".list_name").toggle().html('');
$.each(data.name, function(a,b){
$(".list_name").append('<p>' + data.name + '</p>');
});
},
"error": function (x, y, z) {
// callback to run if an error occurs
alert("An error has occured:\n" + x + "\n" + y + "\n" + z);
}
});
});
this is search result that have problem:
http://i.stac开发者_开发问答k.imgur.com/mObpn.gif
I hope you understand my mean.
$.each
gives you each element once. You need to do
$.each(data.name, function(a,b){
$(".list_name").append('<p>' + b + '</p>');
});
精彩评论