2013-08-02 74 views
0

我有一個自動完成腳本查找Var標籤,我的問題是, 如何用MySQL的結果填充「avaiableTags」?Javascript自動完成列出MySQL結果

$ SQL =「SELECT username FROM users」;例如

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

做一個Ajax請求,或在一次插入從MySQL數據包含這個JS代碼生成的頁面。 –

回答

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']; 
    } 

對於完整的例子請點擊這裏 http://www.daveismyname.com/tutorials/php-tutorials/autocomplete-with-php-mysql-and-jquery-ui/

+0

謝謝,完美的工作 – vonatar