php 由数据库填充的自动完成表单?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6047057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Autocomplete form populated by a database?
提问by captainrad
I am currently working on a project in which I need to have an autocomplete form call its information from a db file. I have seen many tutorials on jquery autocomplete forms, but I do not know how to call a db file to populate the list.
我目前正在开发一个项目,在该项目中我需要一个自动完成表单从 db 文件中调用其信息。我看过很多关于 jquery 自动完成表单的教程,但我不知道如何调用 db 文件来填充列表。
I am working in PHP. Currently the code represents a simple drop down box that is calling on the db file for population.
我在 PHP 工作。目前,代码表示一个简单的下拉框,它调用 db 文件进行填充。
<?php
global $wpdb;
$depts = $wpdb->get_results( "SELECT * FROM departments ORDER BY department_name ASC" );
echo '<select>';
foreach($depts as $row) {
echo '<option name="select_dept" value="'.$row->department_id.'">'.$row->department_name.'</option>';
}
echo '</select>';
?>
Any help would be awesome!
任何帮助都是极好的!
采纳答案by pzinovkin
I recently have used this library for autocompletion - http://www.devbridge.com/projects/autocomplete/jquery/So here is brief script based on yours:
我最近使用这个库进行自动完成 - http://www.devbridge.com/projects/autocomplete/jquery/所以这里是基于你的简短脚本:
<?php
$query = isset($_GET['query']) ? $_GET['query'] : FALSE;
if ($query) {
global $wpdb;
// escape values passed to db to avoid sql-injection
$depts = $wpdb->get_results( "SELECT * FROM departments WHERE department_name LIKE '".$query."%' ORDER BY department_name ASC" );
$suggestions = array();
$data = array();
foreach($depts as $row) {
$suggestions[] = $row->department_name;
$data[] = $row->department_id;
}
$response = array(
'query' => $query,
'suggestions' => $suggestions,
'data' => $data,
);
echo json_encode($response);
} else {
?>
<html>
<body>
<input type="text" name="" id="box" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script src="http://www.devbridge.com/projects/autocomplete/jquery/local/scripts/jquery.autocomplete.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#box').autocomplete({
serviceUrl:'/',
// callback function:
onSelect: function(value, data){ alert('You selected: ' + value + ', ' + data); },
});
});
</script>
</body>
<html>
<?}?>
回答by Rob
Please follow this very well written tutorial
请遵循这个写得很好的教程
回答by Codecraft
JQuery UIincludes an autocomplete, although you still need to write a PHP script to return the information to be added to the control as its done via AJAX. If you know in PHP how to connect to a database, query it, and return a list of the results - then you will have no problems with this. JQuery makes AJAX extremely simple.
JQuery UI包括一个自动完成功能,尽管您仍然需要编写一个 PHP 脚本来返回要添加到控件的信息,就像通过 AJAX 完成的那样。如果您知道如何在 PHP 中连接到数据库、查询它并返回结果列表 - 那么您将没有问题。JQuery 使 AJAX 变得极其简单。
Depending on how complicated your field/data set is - and assuming its not millions upon millions of unindexed records, I would be content to autocomplete from a:
根据您的字段/数据集的复杂程度 - 并假设它不是数百万条未编入索引的记录,我很乐意从以下位置自动完成:
SELECT thing WHERE thing LIKE '".$query."%'
So if you were searching for, say, food... the query "CA" would pull out CArrot and CAbbage and CAuliflower. If you added a % to the beginning of the LIKE, you could get results that contained your query, as opposed to just beginning with it.
因此,如果您正在搜索,比如说,食物……查询“CA”会提取出 CArrot、CAbbage 和 CAuliflower。如果您在 LIKE 的开头添加 %,您可以获得包含您的查询的结果,而不是仅以它开头。
The page your user hits would contain the JQuery part which both sends the request and creates the autocomplete effect from the results, and a very simple, separate PHP script which the AJAX request hits would return the potential 'matches'.
您的用户点击的页面将包含 JQuery 部分,它既发送请求又从结果创建自动完成效果,以及一个非常简单的独立 PHP 脚本,AJAX 请求点击将返回潜在的“匹配”。
Take a look at the Autocomplete demos on JQuery UI