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


how to get value from database with javascript, i am using jquery autocomplete, and i want to search values from database only

我正在使用jquery自动完成

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

以及如何将数据库中的数组值转换为var available Tags。。。谢谢兄弟:D

为了访问数据库并在自动完成中获得值列表,您需要使用服务器端编程语言,就像本例中我们使用的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/