WordPress自动完成搜索:如何连接自定义表的查询文件
WordPress autocomplete search: how to connect query file of custom table?
目的是从自定义表中检索数据,并在运行WordPress的网站上以自动完成的形式显示它。
firm_id firm_name
1 Helsinki genetics
2 23andMe
我使用jQuery-Autocomplete库,并从这个主题中得到建议。这就是我如何填充我的WordPress主题的functions.php
。我想,这可能是错误的。
// add jQuery-Autocomplete library
function my_library_method() {
// register your script location, dependencies and version
wp_register_script('library_script', get_template_directory_uri() . '/js/jquery.autocomplete.js');
// enqueue the script
wp_enqueue_script('library_script');
}
add_action('wp_enqueue_scripts', 'my_library_method');
// add javascript file with autocomplete
function my_scripts_method() {
wp_register_script('custom_script', get_template_directory_uri() . '/js/js_auto.js');
wp_enqueue_script('custom_script');
}
add_action('wp_enqueue_scripts', 'my_scripts_method');
这是输入框的代码。
<input type="text" name="mybox" id="mybox" />
这是一个js_auto.js
文件,它使所有的自动完成魔术。
jQuery(document).ready(function($) {
$('#mybox').autocomplete({
// add the way to the file with database query
serviceUrl: 'tags.php',
// what happens when user chooses autocomlete suggestion
onSelect: function (suggestion) {
alert('You selected: ' + suggestion.value + ', ' + suggestion.data);
}
});
});
最后这是tags.php
文件,其中是查询数据库。
$query = isset($_GET['query']) ? $_GET['query'] : FALSE;
global $wpdb;
// escape values passed to db to avoid sql-injection
$depts = $wpdb->get_results( "SELECT * FROM wp_firms WHERE firm_name LIKE '".$query."%' ORDER BY firm_name ASC" );
$suggestions = array();
$data = array();
foreach($depts as $row) {
$suggestions[] = $row->firm_name;
$data[] = $row->firm_id;
}
$response = array(
'query' => $query,
'value' => $suggestions,
'data' => $data,
);
echo json_encode ($response);
我在没有tags.php
的js_auto.js
文件中测试了手动填充数组的自动补全,它工作得很好。然而,我想从数据库中获取数据数组。
任何想法如何连接这个tags.php
(数据库查询)到js_auto.js
(自动完成脚本)?
对不起,我的解释有点长,但我已经尽可能详细地描述了。我已经在第二个周末试着解决它了,所以任何帮助都会非常感激。
您可能需要将serviceUrl更改为完整路径:
serviceUrl: 'http://yourdomain.com/path/tags.php',
这取决于你在哪里托管tag .php文件。你可以使用
var template_uri = <?php get_template_directory_uri(); ?>;
serviceUrl: template_uri + '/tags.php',
尝试在chrome中使用开发工具,例如,它很可能会告诉你哪里有js错误。
和Replace:
foreach($depts as $row) {
$suggestions[] = $row->firm_name;
$data[] = $row->firm_id;
}
$response = array(
'query' => $query,
'value' => $suggestions,
'data' => $data,
);
:
foreach($depts as $row) {
$data['value'] = $row->firm_name;
$data['data'] = $row->firm_id;
}
$response = array(
'suggestions' => $data;
);
相关文章:
- Dojo不解析自定义小部件的模板html中的小部件声明性
- 在自定义mean.io包中使用angular-chart.js作为依赖项
- jQuery自定义验证比较多个输入的序列
- 从HTTPGET返回一个自定义对象列表,以便在Angular 2应用程序中使用
- 在wordpress一定时间后更改自定义字段
- 表达式引擎扩展开发-向呈现的条目添加自定义javascript
- 自定义函数中的光标位置
- 用于检查数组中是否存在元素的javascript自定义方法
- 绑定时将Parsley minlength消息作为选项传递时,未对其进行自定义
- Meteor-添加用户自定义字段的方法不起作用
- 自定义表单验证和提交
- Javascript/webpack:如何用自定义的文件循环连接目录中的所有json文件
- EventSource连接中的自定义标头
- 节点:建立连接时备份 socket.io 自定义发射器
- 如何将自定义消息处理程序与 Strophe 连接
- 在JavaScript中连接自定义对象数组的字符串
- 无法用自定义分隔符连接javascript数组
- 自定义下拉列表样式(CSS/JS与ASP.. NET c#连接)
- WordPress自动完成搜索:如何连接自定义表的查询文件
- 为Raphael JS涂鸦连接添加自定义移除方法