Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i have this jquery code which poplautes a input selector, which should display like this:

$("#demo-input-pre-populated").tokenInput("http://shell.loopj.com/", {
                prePopulate: [
                    {id: 123, name: "Slurms MacKenzie"},
                    {id: 555, name: "Bob Hoskins"},
                    {id: 9000, name: "Kriss Akabusi"}
                ]
            });

when i try get the vales from the database using php like this:

prePopulate: [
                    <?
                   $responses = array();
                    $topicJSON=getQtopics($getQ);
                    while($row = mysql_fetch_array($topicJSON)){
                    $response = array(
                        'id' => $row['id'],
                        'name' => $row['name']

                    );
                    $responses[] = $response;
                }
                echo json_encode($responses);
 ?>
        ],

which displays the json data like this:

 prePopulate: [
         [{"id":"1","name":"Dormitree"},
         {"id":"1482","name":"carriage of goods"}]        
        ],

but on the #demo-input-pre-populated" input i get undefined, and i think its becuase php is not echoing the json propelrly, how can i fix this thanks :))

share|improve this question

2 Answers

up vote 3 down vote accepted

If you look the two JS outputs, the only difference is that you have two extra enclosing square-brackets [] in the case where you output from PHP - so you're making an array of array of JSON objects in that case while all you need is an array of JSON objects.

Get rid of the enclosing [] for prePopulate because json_encode is already doing that for you:

prePopulate: <?
                $responses = array();
                $topicJSON=getQtopics($getQ);
                while($row = mysql_fetch_array($topicJSON)){
                        $response = array(
                       'id' => $row['id'],
                       'name' => $row['name']
                    );
                    $responses[] = $response;
                }
                echo json_encode($responses);
            ?>,
share|improve this answer
thanks very clean answer, :))) – pingpong Mar 31 '11 at 16:24
can i ask you one more question, would i able to refresh prePoplaute using jquery after a click action i.e. $(".button").click ( //refresh prePoplaute if you get what i mean :)) – pingpong Mar 31 '11 at 16:29
1  
@pingpong - It doesn't look like the plugin supports this, you might check with the author or put in a feature request. If you look at the source, the insert_token() function inserts new tokens but I'm not proficient enough with JavaScript to figure out how (if possible) to get a reference to that function and invoke it with new values. – no.good.at.coding Mar 31 '11 at 17:04

Your prePopulate variable is an array containing an array of objects, but you just want it to be an array of objects.

You can either take off the [ and ] brackets before and after the PHP block, or echo this in your PHP block: array_pop(json_decode($responses))

share|improve this answer

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.