Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a list of guests that I want to be able to use in the jQuery autocomplete but I can't seem to get the format correct. I'm using CakePHP 2.3.8 and have a list of guests sent from my controller to view. When I type into the text box, I get the following error

//in reference to source of the elements to populate the autocomplete list
Property 'source' of object [object Object] is not a function 

I searched for this error on here, but I couldn't find anything related to converting a PHP array while also accounting for the index number of that item. I have a PHP array in the format of

echo "<pre>"; print_r($guest_list); echo "</pre>"; 
//results in the following, where the index is the guest's ID
array(
    [1] => guest a,
    [2] => guest b,
    [3] => guest c
)

Keep in mind, the index refers to the guest's ID.

I used json_encode to convert the php $guests array which results in the following format

var guests = {"1":"guest a","2":"guest b","3":"guest c"};

I noticed in the documentation the source of the data is usually in a format like

var guests = [guest_a, guest_b, guest_c];

Keep in mind index refers to the guest's ID, so I need to use this number to make sure the correct guest is chosen, if there happens to be two guests with the same name

So, here's the javascript/jquery for the autocomplete box

var guests = <?php echo json_encode($guest_list); ?>;
$(function() {
    $( "#guests_autocomplete" ).autocomplete({
      source:  guests
    });
});

How can I properly format my guests array to work with autoComplete and still keep track of the guest's id?

share|improve this question

1 Answer 1

use

var guests = ["<?php echo  implode('","',$guest_list); ?>"];

instead of

var guests = <?php echo json_encode($guest_list); ?>;

and if guest names are unique you can use array_search to find id

share|improve this answer
    
It's saying unexpected identifier. However, there's another issue. It was my fault for not stating that guests are in Firstname, Lastname format. I guess I may have to re-structure it, or just remove the comma between the first and last name. –  user2443591 Sep 8 '13 at 18:35
    
you don't need to change format, so i changed the code to put name,lastame in quote. please use the new code –  Tufan Barış Yıldırım Sep 8 '13 at 19:05
    
Thanks, although this works I still need to keep track of the guests Id as I mentioned in the post. Can I somehow append a hidden field to this? Previously I used a select list and would send the guest's ID instead of their name upon form submission. Can I do the same with this? Upon form submission I need to send the guest's ID which is the index value of the array. I really appreciate your help –  user2443591 Sep 8 '13 at 19:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.