0

I'm trying to get the jquery ui autocomplete to work with a codeigniter project.

so far I have an input field <input type="text" id="text1"/>

and then in my script I have

source: function(request, response) {
            $.post('autocompleteHandler', {mechanic: request.term}, function(data) {
            console.log('data.phpResp = '+data.phpResp);
            console.log('in post?');

            console.log('data = '+data.toSource);

            var realArray = $.makeArray(data);  // this line was needed to use the $.map function

            response($.map(realArray, function(item) {

                console.log('in map');

                return {
                    label: item.info,
                    value: item.info
                }

            }));


            }, 'json');
        },

In my codeigniter controller I have this function

function autocompleteHandler() {

    $input = $this->input->post('mechanic');

    $this->load->model('login_model');

    $results = $this->login_model->search_mechanic_criteria($input);

    $mechs= array();
    foreach($results as $result) {

        $mechs['info'] = $result['mechanic_name'];  

    }
}

I'm not getting this to work. anyone have any ideas of where I can begin to troubleshoot? I really have a hard time with the jquery ui documentation.

EDIT: I've changed my code a bit. Instead of returning json_encode, I needed to echo json_encode on the php side of things. I still don't have anything showing up in my console though.

2ND EDIT Now my question is, how can I return multiple values for the autocomplete function? If i have a query that returns, just one row, it works fine, but if I have multiple rows returned, doesn't work. It's gotta be something with the way i'm returning the data, but I can't figure it out.

5
  • Please check view.jquery.com/trunk/plugins/autocomplete/demo, i believe you're not returning the right format from PHP. Use firebug to see the ajax content returned. Commented Aug 26, 2011 at 20:36
  • I'm not using that plugin. I'm using the jquery ui autocomplete plugin. Commented Aug 26, 2011 at 20:37
  • did you try fiddler to see what the server was producing? Commented Aug 27, 2011 at 18:29
  • I don't need fiddler, I can just use the net console in firebug. I can see that the server is returning what i'm expecting. Commented Aug 29, 2011 at 14:35
  • For the 2ND EDIT question, see jsfiddle.net/XYMGT/1. It is a matter of returning an array of objects instead of just an object, at least while speaking client side. Server side it is properly a matter of using "$mechs[] = $result['mechanic_name'];" instead of "$mechs['info'] = $result['mechanic_name'];". Commented Aug 30, 2011 at 21:05

2 Answers 2

1

I have been playing around with jsfiddle after you mentioned toSource(). See http://jsfiddle.net/XYMGT/. I find that the map function does not return jQuery, but the new array.

OLD STUFF:

I suspect that the $.map function does not return the array, but jQuery. Maybe it would to do this:

            // also you could inspect the data if the server returns what you think it returns:
            console.log(data);

            // first map the array
            $.map(data, function(item) {

                console.log('in response?');


                return {
                    label: 'testing',
                    value: 'test'
                }
            })
            // ...then separately do the response part
            response(data);

Lets us know if it makes a difference.

EDIT:

If this PHP code is still used:

function autocompleteHandler() {
  echo json_encode(array('phpResp' => 'something'));
}

Then console.log(data) should show the following in the console tab in FireBug:

{'phpResp':'somehting'}

Meaning that console.log(data.phpResp) should print 'something'. I am unsure where you are getting data.toSource from.

Sign up to request clarification or add additional context in comments.

5 Comments

Good, what about the first console.log statement, what did it print? undefined, null, [] or the expected data?
if I change it from console.log(data); to console.log(data.toSource);, it prints undefined.
console.log(data.phpResp) prints something as expected. toSource is a way to view the data of an object in js.
I've updated my question with new code that works most of the way. Check it out and see if you can help me with my current problem.
I figured it out from this link. Inside my foreach loop, I just needed this line - array_push($mechs, array('info'=>$result['mechanic_name']));. Thanks for your help.
0

I would launch fiddler and see what it says it's returning. You can also go straight to your server side page in the browser that is serving the JSON results. I think the autocomplete automatically adds ?term to the string. url.aspx?term=|valueofText1|

$("#text1").autocomplete({
    source: url,
    minLength: 2,
    select: function (event, ui) {
        sou = ui.item.label;
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.