使用JSON,PHP和MYSQL进行自动完成,而不是优化结果(Autocomplete using JSON, PHP & MYSQL not refining results)

编程入门 行业动态 更新时间:2024-10-26 18:30:28
使用JSON,PHP和MYSQL进行自动完成,而不是优化结果(Autocomplete using JSON, PHP & MYSQL not refining results)

我正在尝试使用JSON来建议来自mysql数据库的搜索项目,但是当我在输入框中输入任何内容时,数据库中的所有结果都会显示,并且根据输入文本它们不会被细化。

这是我的html / php UI页面:

<!-- Start of content --> <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> <script src="//code.jquery.com/jquery-1.10.2.js"></script> <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> <script> $(function() { $( "#searchText" ).autocomplete({ source: 'includes/functions/json_search.php', minLength:'2' }); }); </script> <div class="ui-widget"> <label for="searchText">Search the menu: </label> <input id="searchText" name="searchText"> </div>

这是我的PHP:

<?php include_once ("../../config/init.php"); //get search term $searchTerm = $_GET['searchText']; //get matched data from menu table $query = $connection->query("SELECT * FROM menu WHERE name LIKE '%".$searchTerm."%' ORDER BY name ASC"); while ($row = $query->fetch_assoc()) { $data[] = $row['name']; } //return json data echo json_encode($data); ?>

任何人都可以建议为什么它不会改进结果? 谢谢

I am trying to use JSON to suggest search items from the mysql database in php but when I type anything in the input box, all results from the database are showing and they are not being refined depending on the input text.

Here is my html/php UI page:

<!-- Start of content --> <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> <script src="//code.jquery.com/jquery-1.10.2.js"></script> <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> <script> $(function() { $( "#searchText" ).autocomplete({ source: 'includes/functions/json_search.php', minLength:'2' }); }); </script> <div class="ui-widget"> <label for="searchText">Search the menu: </label> <input id="searchText" name="searchText"> </div>

Here is my php:

<?php include_once ("../../config/init.php"); //get search term $searchTerm = $_GET['searchText']; //get matched data from menu table $query = $connection->query("SELECT * FROM menu WHERE name LIKE '%".$searchTerm."%' ORDER BY name ASC"); while ($row = $query->fetch_assoc()) { $data[] = $row['name']; } //return json data echo json_encode($data); ?>

can anyone suggest why it will not refine the results? thanks

最满意答案

//get search term $searchTerm = $_GET['searchText'];

应该:

$searchTerm = $_GET['term'];

每个jqueryui自动完成文档

自动完成插件不会过滤结果,而是使用术语字段添加查询字符串,服务器端脚本应使用该字段来过滤结果。

//get search term $searchTerm = $_GET['searchText'];

should be:

$searchTerm = $_GET['term'];

Per jqueryui autocomplete doc

The Autocomplete plugin does not filter the results, instead a query string is added with a term field, which the server-side script should use for filtering the results.

更多推荐

本文发布于:2023-07-23 01:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1225597.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自动完成   而不是   MYSQL   JSON   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!