Ankur
Ankur

Reputation: 169

Get dynamic values in jQuery autocomplete

so I have this code which I'm using for a search box(like we have in google, the autosearch). In this below example, the values are hard-coded, but, I would need to have dynamic values in the array.

Here's the code :-

<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery UI Autocomplete - Default functionality</title>
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script>
  $(function() {

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

</head>
<body>

<div class="ui-widget">
  <label for="tags">Tags: </label>
  <input id="tags">
</div>


</body>
</html>

How would I have a dynamic array in this place? You may suppose that I have a variable $row which gets the data(search options) dynamically from a mysql database.

Thank you.

Edit :-

The php code which fetches the value :-

<?php
include 'dbconnector.php';
  $query="SELECT firstname from users order by userid";
  $result=mysql_query($query,$db) or die (mysql_error($db));
  $row=mysql_fetch_array($result);

?>

Upvotes: 1

Views: 1808

Answers (1)

JamesG
JamesG

Reputation: 1697

jQuery UI Autocomplete has a 'remote' data function documented here. The simplest version of this is:

$( "#tags" ).autocomplete({
  source: 'script.php'
});

script.php should return JSON data. So you would create an array from your $row(s), json_encode it and return it back.

As promised, here's a rough example using your code as a basis. Disclaimers: I'm not 100% familiar with the format that jQuery UI is expecting, and you should be aware that mysql_query() is deprecated and you should explore the alternatives such as mysqli or PDO.

Updated answer to show use of $_GET['term'] which jQuery UI sends. I'm assuming you're looking for names? I'm using the DB column 'firstname'.

HUGE disclaimer: As you're now passing user-generated strings into your database query, ensure that you sanitise properly. I've used mysql_real_escape_string() to match your example but it is deprecated and you should investigate the alternatives as above.

// Sanitise GET var
$term = mysql_real_escape_string($_GET['term']);

// Add WHERE clause
$query = "SELECT `id`, `firstname` FROM `users` WHERE `firstname` LIKE '%".$term."%' ORDER BY `id`";

$result = mysql_query($query,$db) or die (mysql_error($db));

while($row = mysql_fetch_array($result)){
        
    $array[$row['id']] = $row['firstname'];
        
}
    
header('Content-type: application/json');
print json_encode($array);
exit(); // AJAX call, we don't want anything carrying on here

Upvotes: 3

Related Questions