FahadAkram
FahadAkram

Reputation: 465

TypeaHead Not working on getting data from database

I am working on jquery typeahead

 $('#country_v1-query<?= $i  ?>').typeahead({
                            order: "desc",
                                    source: {
                                    data: [
                                            "Afghanistan", "Albania", "Algeria", "Afdorra"
                                    ]
                                    },
                                    callback: {
                                    onInit: function(node) {
                                    console.log('Typeahead Initiated on ' + node.selector);
                                    }
                                    }
                            });

I want to pass data coming from php like

for ($i = 1; $i <= 10; $i++) {
$ar = array('apple', 'orange', 'banana', 'strawberry');
echo json_encode($ar);

i want to show this data (json_encode($ar)) in typeahead like

data:[<?php echo json_encode($ar);?>] 

but when i include header of json it render page as json and stop loading HTML and CSS Thanks in Advance

Upvotes: 0

Views: 55

Answers (1)

davidkonrad
davidkonrad

Reputation: 85538

Using implode() you could output the PHP array as a javascript array in one line :

data: <? echo '["'.implode('","', $ar).'"]'; ?> 

will produce

data: ["apple","orange","banana","strawberry"]

Upvotes: 1

Related Questions