简体   繁体   English

在jQuery自动完成中获取动态值

[英]Get dynamic values in jQuery autocomplete

so I have this code which I'm using for a search box(like we have in google, the autosearch). 所以我有这个代码,我用于搜索框(就像我们在google,autosearch)。 In this below example, the values are hard-coded , but, I would need to have dynamic values in the array. 在下面的示例中,值是硬编码的 ,但是,我需要在数组中包含动态值。

Here's the code :- 这是代码: -

<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery UI Autocomplete - Default functionality</title>
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script>
  $(function() {

    var availableTags = [
      "ActionScript",
      "AppleScript",
      "Asp",
      "BASIC",
      "C",
      "C++",
      "Clojure",
      "COBOL",
      "ColdFusion",
      "Erlang",
      "Fortran",
      "Groovy",
      "Haskell",
      "Java",
      "JavaScript",
      "Lisp",
      "Perl",
      "PHP",
      "Python",
      "Ruby",
      "Scala",
      "Scheme"
    ];
    $( "#tags" ).autocomplete({
      source: availableTags
    });
  });
  </script>

</head>
<body>

<div class="ui-widget">
  <label for="tags">Tags: </label>
  <input id="tags">
</div>


</body>
</html>

How would I have a dynamic array in this place? 我如何在这个地方拥有动态阵列? You may suppose that I have a variable $row which gets the data(search options) dynamically from a mysql database. 您可以假设我有一个变量$ row ,它从mysql数据库动态获取数据(搜索选项)。

Thank you. 谢谢。

Edit :- 编辑: -

The php code which fetches the value :- 获取值的PHP代码: -

<?php
include 'dbconnector.php';
  $query="SELECT firstname from users order by userid";
  $result=mysql_query($query,$db) or die (mysql_error($db));
  $row=mysql_fetch_array($result);

?>

jQuery UI Autocomplete has a 'remote' data function documented here . jQuery UI Autocomplete具有此处记录的“远程”数据功能。 The simplest version of this is: 最简单的版本是:

$( "#tags" ).autocomplete({
  source: 'script.php'
});

script.php should return JSON data. script.php应该返回JSON数据。 So you would create an array from your $row(s), json_encode it and return it back. 因此,您将从$ row(s)创建一个数组,json_encode它并将其返回。

As promised, here's a rough example using your code as a basis. 正如所承诺的,这是一个使用您的代码作为基础的粗略示例。 Disclaimers: I'm not 100% familiar with the format that jQuery UI is expecting, and you should be aware that mysql_query() is deprecated and you should explore the alternatives such as mysqli or PDO . 免责声明:我不是100%熟悉jQuery UI期望的格式,你应该知道不推荐使用 mysql_query(),你应该探索mysqliPDO等替代品。

Updated answer to show use of $_GET['term'] which jQuery UI sends. 更新了答案,显示jQuery UI发送的$ _GET ['term']的使用。 I'm assuming you're looking for names? 我假设你正在寻找名字? I'm using the DB column 'firstname'. 我正在使用DB列'firstname'。

HUGE disclaimer: As you're now passing user-generated strings into your database query, ensure that you sanitise properly. 巨大的免责声明:由于您现在将用户生成的字符串传递到数据库查询中,请确保正确清理。 I've used mysql_real_escape_string() to match your example but it is deprecated and you should investigate the alternatives as above. 我已经使用mysql_real_escape_string()来匹配您的示例,但它已被弃用,您应该如上所述调查替代方案。

// Sanitise GET var
$term = mysql_real_escape_string($_GET['term']);

// Add WHERE clause
$query = "SELECT `id`, `firstname` FROM `users` WHERE `firstname` LIKE '%".$term."%' ORDER BY `id`";

$result = mysql_query($query,$db) or die (mysql_error($db));

while($row = mysql_fetch_array($result)){

    $array[$row['id']] = $row['firstname'];

}

header('Content-type: application/json');
print json_encode($array);
exit(); // AJAX call, we don't want anything carrying on here

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM