gpt4 book ai didi

jquery - 如何执行实时查询并以自动完成方式显示数据

转载 作者:行者123 更新时间:2023-11-29 12:47:33 24 4
gpt4 key购买 nike

我的index.html文件

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Demo</title>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/themes/base/minified/jquery-ui.min.css" type="text/css" />
</head>
<body>

<form action='' method='post'>
<p><label>Country:</label><input type='text' name='country' value='' class='auto'></p>
</form>

<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"> </script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.1/jquery-ui.min.js"></script>
<script type="text/javascript">
$(function() {

//autocomplete
$(".auto").autocomplete({
source: "search.php",
minLength: 1
});

});
</script>
</body>
</html>

我的 search.php 文件

<?php

$con=mysqli_connect("localhost","root","something","hindi_test");

// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_set_charset ( $con , 'utf8' );

$result = mysqli_query($con,"SELECT A FROM data ");

while($row = $result->fetch_object()){
$user_arr[] = $row->A;


}
echo json_encode($user_arr);

mysqli_close($con);

?>

这段代码工作正常。它显示“A”列的所有数据成员。现在我想根据用户输入数据显示自动完成功能,以便在输入更多字符时过滤数据。如何在我的 search.php 文件中执行此操作?

最佳答案

根据autocomplete jquery documentation

a query string is added with a term field, which the server-side script should use for filtering the results. For example, if the source option is set to "http://example.com" and the user types foo, a GET request would be made to http://example.com?term=foo

您可以在服务器端读取查询字符串并使用术语值过滤查询

您的查询可能是这样的:

$result = mysqli_query($con,"SELECT A FROM data WHERE A LIKE %". $_GET["term"] ."%");

当然,您必须清理 $_GET["term"] 以防止 SQL 注入(inject)攻击。

关于jquery - 如何执行实时查询并以自动完成方式显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25235329/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com