我有一个搜索框,通过数据库进行搜索.在我的代码中,搜索在一个输入框中完成,动态搜索输出显示在它下面的文本区域中.
我想要的是像谷歌这样的搜索,当用户星星打字时,它应该显示数据库表中的类似项目.
例如,如果我有两个名为“Dummy 1”和“Dummy 2”的组织,并且用户键入“du”,则搜索栏应显示2个结果,用户应该能够选择一个.
我的代码是:
Brand Name:
Search for an Organization: <
Selected Organization:
js是这样的:
function searchq(){
var searchTxt = $("input[name='search']").val();
$.post("search.php", {searchVal: searchTxt},function(output){
$("#output").html(output);
}
}
这是search.php文件:
include 'db_connect.php';
$link = mysqli_connect($host, $username, $password, $db);
if(!link){
echo "DB Connection error";
}
$output = '' ;
$output2 = '' ;
if (isset($_POST['searchVal'])){
$searchq = $_POST['searchVal'];
//$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$query = mysqli_query($link, "SELECT * FROM `organisations_info` WHERE `Organisation_Name` LIKE '%$searchq%'")or die("Could not search!");
$count = mysqli_num_rows($query);
if($count == 0){
$output = '
}else{
while($row = mysqli_fetch_array($query)){
$orgname = $row['Organisation_Name'];
$orgid = $row['Organisation_Id'];
$subs = $row['Subscription_Type'];
//$output = echo "" . $orgname . "";
$output = $orgname;
$output2 = $orgid;
$output3 = $subs;
//$output = '
}
}
}
echo ($output);
?>
我怎样才能做到这一点?