如何使用javascript从数据库获取值,我使用jquery自动完成,我想只从数据库中搜索值

时间:2015-02-09 05:06:56

标签: javascript php jquery

使用jquery自动完成

<script>
$(function() {    
    var availableTags = [
      "ActionScript",
      "AppleScript",
      "Scheme"
      ];    
$( "#tags" ).autocomplete({
    source: availableTags  });});
</script>

如何从数据库中获取数组值到var可用标签... 谢谢兄弟:D

1 个答案:

答案 0 :(得分:0)

为了访问数据库并获取自动填充中的值列表,您需要使用服务器端编程语言,因为在此示例中我们使用php。

$("#autocomplete").autocomplete({
source: "search.php",
minLength: 2,//search after two characters
select: function(event,ui){
    //do something
    }
});

在search.php之后将异步调用source将从数据库中提取此数据。

source: [{"value":"Some Name","id":1},{"value":"Some Othername","id":2}]

我举一个PHP代码应该是

的例子
//connect to your database

$term = trim(strip_tags($_GET['term']));//retrieve the search term that autocomplete sends

$qstring = "SELECT description as value,id FROM test WHERE description LIKE '%".$term."%'";
$result = mysql_query($qstring);//query the database for entries containing the term

while ($row = mysql_fetch_array($result,MYSQL_ASSOC))//loop through the retrieved values
{
        $row['value']=htmlentities(stripslashes($row['value']));
        $row['id']=(int)$row['id'];
        $row_set[] = $row;//build an array
}
echo json_encode($row_set);//format the array into json data

祝你有更多信息http://www.simonbattersby.com/blog/jquery-ui-autocomplete-with-a-remote-database-and-php/