1

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

?>
3
  • Show us your current php code, please! :) Commented Feb 9, 2014 at 19:19
  • @DanielLisik Updated with the php code. Commented Feb 9, 2014 at 19:23
  • You'll need to encode your results, $row to json in a php file then use that php file as the source for the auto-complete. Use this tutorial as a reference bit.ly/1gj2DDM Commented Feb 9, 2014 at 19:51

1 Answer 1

3

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

5 Comments

Is there any example for the script.php file? I'm really new in JSON.
Thank you for posting this. I'll try this out and get back to you.
Works good, but for whatever I type..it throws all the values in the database. How could that be fixed?
That worked pretty well. Thank you. This was the 50% of my problem. The next 50% is, how would I redirect the user when he clicks on a particular search result? Suppose, I would like to redirect him to : page.php?id=(the id we get from the query on the search.php page).

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.