开发者

my problem with json_encode()? [closed]

开发者 https://www.devze.com 2023-03-20 07:44 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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>');
        });   
0

精彩评论

暂无评论...
验证码 换一张
取 消