user1080247
user1080247

Reputation: 1166

Why don't I see any value from autocomplete search in jQuery

Why can't I see any value suggestion from text input. I already use autocomplete code with JSON and jQuery. But no value is displayed in the text input

bookstore/views/admin/auto_complete.php

<script>
    $('#swSearch').keypress(function () {
        var dataObj = $(this).closest('form').serializeArray();
        $.ajax({
            url: 'http://localhost/boostore/admin_d_book_groups/search',
            data: dataObj,
            dataType: 'json',
            success: function (data) {
                $("#suggestion_tab").html('');
                $.each(data.name, function (a, b) {
                    $("#suggestion_tab").append('<li>' + data.b + '</li>');
                });
                // Display the results
                ///alert(data);
            },
            "error": function (x, y, z) {
                // callback to run if an error occurs
                alert("An error has occured:\n" + x + "\n" + y + "\n" + z);
            }
        });
    });  
</script>
<div id="swSearch">
<form>
    <input type="text" value="" id="swSearch"  class="swSearch" />
</form>
    <div class="suggestion_tab" id="suggestion_tab"></div>

</div>

Admin_d_book_groups controller

function search(){
        $searchterm = $this->input->post('search_hotel');
        echo json_encode($this->d_book_groups->sw_search($searchterm));
    }  

d_book_groups_model

function sw_search($searchterm)
    {
         $query = $this->db->order_by("bg_id", "desc")->like('bg_name', $searchterm, 'after')->get('d_book_groups');
        $data = array();
        foreach ($query->result() as $row)
        {
           $data[] = $row->bg_name;
        }
        return $data;             
        //return mysql_query("select * from hotel_submits where name LIKE '".$searchterm."'");
    }  

Upvotes: 3

Views: 349

Answers (2)

Brendan
Brendan

Reputation: 4565

Is $config['compress_output'] = TRUE; in your application/config/config.php?

If it is, you can't output content directly from the controller methods. You must use a view. You can create a view as simple as:

<?php echo $response;

and just pass the json data like this:

function search() 
{
    $searchterm = $this->input->post('search_hotel');
    $data['response'] = json_encode($this->d_book_groups->sw_search($searchterm));
    $this->load->view('ajax/json_response', $data);
}

Upvotes: 2

Hamidreza
Hamidreza

Reputation: 1915

I dont know why, but you make a mistake in this code:

$.each(data.name, function (a, b) {
    $("#suggestion_tab").append('<li>' + data.b + '</li>');
});

You can not use "data.b" in $.each method, b is an object of data.name for more help, actually "b" equal to data.name[a] (data.name[a] == b)

So data.b is false, (data.name[a] == b) && data.name[a] != data.b

Use console.log(b) in $.each loop to see your array object (you need firefox and firebug extention)

$.each(data.name, function (a, b) {
    console.log(b);
});

I think you want to put each name, in li element, if its true, use this code:

$.each(data, function (a, b) {
    $("#suggestion_tab").append('<li>' + b.name + '</li>');
});

// here b.name == data[a].name

Upvotes: 0

Related Questions