vonatar
vonatar

Reputation: 64

Javascript Autocomplete list a MySQL result

I have a Autocomplete script that look for Var tags, my question is, How can i populate the "avaiableTags" with a result of MySQL?

$SQL = "SELECT username FROM users"; for example

(function($) {
    $(document).ready(function(e) {
        if($.fn.autocomplete) {
            var availableTags = [
                "User 1", //mysql result here
                "User 2",
                "ecc..."
            ];

Upvotes: 0

Views: 2399

Answers (1)

usman allam
usman allam

Reputation: 284

//javascript
   <script type="text/javascript">
   $(function() {

    //autocomplete
    $(".auto").autocomplete({
        source: "search.php",
        minLength: 1
    });                

   });
  </script>

//search.php something like this

   $stmt = $conn->prepare('SELECT country FROM countries WHERE country LIKE :term');
   $stmt->execute(array('term' => '%'.$_GET['term'].'%'));

   while($row = $stmt->fetch()) {
   $return_arr[] =  $row['country'];
   }

For full example please check here http://www.daveismyname.com/tutorials/php-tutorials/autocomplete-with-php-mysql-and-jquery-ui/

Upvotes: 1

Related Questions