0

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..."
            ];
1
  • do an ajax request, or insert the data from mysql at the time the page that contains this JS code is generated. Commented Aug 2, 2013 at 15:20

1 Answer 1

1
//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/

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

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.