1

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 :))

2 Answers 2

3

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);
            ?>,
Sign up to request clarification or add additional context in comments.

2 Comments

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 - 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.
2

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))

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.