7

I have the following data currently feeding 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 modify this to retrieve the array of results from a php page. Can somebody help me to modify this so I can in JavaScript do this?

network_autocomplete=data

Currently, the code retrieves data from an SQLite table in this manner:

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'));
}
2
  • SELECT net, name, FROM network have unuse comma , change to SELECT net, name FROM network Commented Jun 30, 2011 at 13:47
  • Why you echo and return at the same time? Commented Jun 30, 2011 at 13:50

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

What I do when i need this is put some PHP directly in the 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

1 Comment

If there’s any chance at all that the data could contain quotes or other non-JavaScript-string-friendly characters, concatenating strings together like this could break, or, at worst, leave you open to XSS attacks. Use json_encode, its output is perfectly OK to embed in JavaScript.
0

You can do the folowing:

<?php
$values = array("'foo'", "'bar'", "'baz'"); ?>

<script>
    $(function() {
        var network_autocomplete = [
          <?php echo join(",", $values) ?>
        ];
    ...
</script>

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.