How to get a PHP script to transfer data to jQuery autocomplete?

I have the following data that is currently submitting jQuery autocomplete

var network_autocomplete=[ "ActionScript", "AppleScript", "Asp", "BASIC", "C", "C++", "Clojure", "COBOL", "ColdFusion", "Erlang", "Fortran", "Groovy", "Haskell", "Java", "JavaScript", "Lisp", "Perl", "PHP", "Python", "Ruby", "Scala", "Scheme" ]; $( "#network" ).autocomplete({ source: network_autocomplete }); 

I need to change this to get an array of results from a php page. Can someone help me change this so that I can do this in JavaScript?

 network_autocomplete=data 

Currently, the code retrieves data from an SQLite table as follows:

 try { $db = new PDO('sqlite:/tmp/bacnet.db'); $query = "SELECT net, name, FROM network"; foreach ($db->query($query) as $row) { $networks[] = array('net' => $row['net']); } echo json_encode($networks); return json_encode($networks); } catch (PDOException $e) { echo json_encode(array('message' => 'Could not connect to the database')); } 
+7
source share
3 answers

Change $networks[] = array('net' => $row['net']); on
$networks[] = $row['net']; And you should not have a return if it is not a function (and if it is a function, why does it have an echo?)

+1
source

What I do when I need it, I put PHP right in javascript, so

 foreach ($db->query($query) as $row) { $networks[] = '{ label: "blah", value: "' . $row['net'] . '" }'; } $autocomplete = implode(",",$networks); 

Then inside the script you can do

 var network_autocomplete = [$autocomplete]; $( "#network" ).autocomplete({ source: network_autocomplete }); 

I hope this helps

0
source

You can do the following:

 <?php $values = array("'foo'", "'bar'", "'baz'"); ?> <script> $(function() { var network_autocomplete = [ <?php echo join(",", $values) ?> ]; ... </script> 
0
source

All Articles