jQuery UI自动完成显示“无搜索结果”
问题描述:
I want to display suggestions from mysql database using jQuery UI Autocomplete
I have following form
<form action="search.php" method="POST">
<input type="text" name="search" id="search-input">
<input type="submit" value="Submit" id="submit">
</form>
search.php
<?php
require_once 'db.php';
$a = array();
if (isset($_POST['search']) && !empty($_POST['search'])) {
$search_param = trim($_POST['search']);
$slct_search = $db->prepare("SELECT student_name FROM student_details WHERE student_name LIKE ?") or die($db->error);
$slct_search->bind_param('s', $search_param);
$slct_search->execute();
$res = $slct_search->get_result();
if($res->num_rows) {
while ($result = $res->fetch_object()) {
$a[] = $result->student_name;
}
echo json_encode($a);
} else {
echo 'OOPS we had a problem';
}
}
?>
The search.php working fine. it returns
["ravi","ravi"]
JS Code
$(function() {
$("#search-input").autocomplete({
source: "search.php",
minLength: 2
});
});
The Problem is when I start type in text box immediately displayed
No search results.
i also tried JQuery UI Autocomplete Search Results do not display
我想使用jQuery UI Autocomplete显示来自mysql数据库的建议 p>
I 有以下表格 p>
&lt; form action =“search.php”method =“POST”&gt;
&lt; input type =“text”name =“search”id =“search-input”&gt;
&lt; input type =“submit”value =“提交”id =“提交”&gt;
&lt; / form&gt;
code> pre>
\ n search.php p>
&lt;?php
require_once'db.php';
$ a = array();
if( isset($ _ POST ['search'])&amp;&amp;!empty($ _ POST ['search'])){
$ search_param = trim($ _ POST ['search']);
$ slct_search = $ db-&gt; prepare(“SELECT student_name FROM student_details WHERE student_name LIKE?”)或死($ db-&gt;错误);
$ slct_search-&gt; bind_param('s',$ search_param);
$ slct_search - &GT;执行();
$ res = $ slct_search-&gt; get_result();
if($ res-&gt; num_rows){
while($ result = $ res-&gt; fetch_object()){
$ a [] = $ result-&gt; student_name;
}
echo json_encode ($ a);
}其他{
echo'OOPS我们遇到了问题';
}
}
?&gt;
code> pre>
search.php工作正常。 它返回 p>
[“ravi”,“ravi”] p>
blockquote>
JS代码 p>
$(function(){
$(“#search-input”)。autocomplete({
source:“search.php”,
minLength:2
}) ;
});
code> pre>
问题是当我开始输入文本框时立即显示 p>
没有搜索结果。 p>
blockquote>
我也试过 JQuery UI自动完成搜索结果不显示 p>
div>
答
HTML
<form action="" method="">
<input type="text" name="search" id="search-input" autocomplete="off">
<input type="submit" value="Submit" id="submit">
<div id="empty-message"></div>
</form>
Now search.php
$searchTerm = trim($_GET['term']);
$query = $db->query("SELECT student_name FROM student_details WHERE student_name LIKE '%".$searchTerm."%' ORDER BY student_name ASC");
while ($row = $query->fetch_object()) {
$data[] = $row->student_name;
}
echo json_encode($data);
jquery ui autocomplete only working with $_GET
So i am using $_GET['term'], See below Picture
JS code
$('#search-input').autocomplete({
source: 'search.php',
minLength: 2,
response: function(event, ui) {
// ui.content is the array that's about to be sent to the response callback.
if (ui.content.length === 0) {
$("#empty-message").text("No results found");
} else {
$("#empty-message").empty();
}
}
});