基于SQL数据库的自动完成文本框结果 [英] Autocomplete Textbox results based from SQL database
问题描述
我试图在文本框中创建一个自动完成函数,但结果应该来自我的SQL数据库。
这是我正在尝试的代码配置:
index.php :
< html lang = EN >
< head>
< meta charset =utf-8>
< title> jQuery UI自动完成 - 默认功能< / title>
< link rel =stylesheethref =http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css>
< script src =http://code.jquery.com/jquery-1.9.1.js>< / script>
< script src =http://code.jquery.com/ui/1.10.4/jquery-ui.js>< / script>
< link rel =stylesheethref =/ resources / demos / style.css>
< script>
$(function(){
var availableTags = [
autocomplete.php;];
$(#tags).autocomplete({
source :availableTags
});
});
< / script>
< / head>
< body>
< div class =ui-widget>
< label for =tags>标记:< / label>
< input id =tags>
< / div>
< / body>
< / html>
编辑:我更改了变量availableTags的内容, code> var availableTags =<?php include('autocomplete.php')?> ;;
变量 availableTags 是单词的来源,所以我尝试改变它,而是将文件名从数据库中提取出来。
这是我的 autocomplete.php 文件:
<?php
include 'conn.php');
$ sql =SELECT * FROM oldemp;
$ result = mysqli_query($ mysqli,$ sql)或die(mysqli_error());
while($ row = mysqli_fetch_array($ result))
{
echo''。$ row ['name']。',;
}
?>
编辑:还改变了while循环的内容并将其
$ name = mysqli_real_escape_string($ con,$ row ['name']);
$ json [] = $ name;
如何将 autocomplete.php 中的取词插入 > availableTags 变量?
编辑/更新:每当我在文本框中键入内容时都会显示一个列表,没有文字。我知道它在提取,但这个词本身没有显示在列表中。
解决方案解决我的问题。让脚本如下所示:
<! - 无此三下, AUTOCOMPLETE将会看起来很糟糕,或者根本无法工作 - >
< link rel =stylesheethref =http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css>
< script src =http://code.jquery.com/jquery-1.9.1.js>< / script>
< script src =http://code.jquery.com/ui/1.10.4/jquery-ui.js>< / script>
< script>
$ b $(function(){
$(#tags).autocomplete({
source:autocomplete.php
});
});
< / script>
和 autocomplete.php (我们将获取数据填充自动填充输入字段):
<?php
include( conn.php); / *在此文件中建立连接;请确认它是mysqli_ * * /
$ stmt = $ con> prepare(SELECT description FROM table); / * START PREPARED STATEMENT * /
$ stmt-> execute(); / *执行查询* /
$ stmt-> bind_result($ description); / *将结果绑定到这个变量* /
while($ stmt-> fetch()){/ *获取所有结果* /
$ description_arr [] = $ description; / *将这个变量的每个结果存储在数组中* /
} / *结束时LOOP * /
echo json_encode($ description_arr); / * ECHO所有结果* /
?>
I'm trying to create an auto-complete function into a textbox but the result should come from my SQL database.
Here's the code that i'm trying to configure:
index.php:
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Autocomplete - Default functionality</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script>
$(function() {
var availableTags = [
"autocomplete.php"; ];
$( "#tags" ).autocomplete({
source: availableTags
});
});
</script>
</head>
<body>
<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags">
</div>
</body>
</html>
EDIT: I changed the content of variable availableTags and made it into var availableTags = <?php include('autocomplete.php') ?>;
Variable availableTags is the source of words, so I try to change it and instead put a file name where fetching of words from my database is happening.
Here's my autocomplete.php file:
<?php
include('conn.php');
$sql="SELECT * FROM oldemp";
$result = mysqli_query($mysqli,$sql) or die(mysqli_error());
while($row=mysqli_fetch_array($result))
{
echo "'".$row['name']."', ";
}
?>
EDIT: Also changed the content of the while loop and made it into
$name=mysqli_real_escape_string($con,$row['name']);
$json[]=$name;
How can I insert the fetched words from autocomplete.php into availableTags variable?
EDIT/UPDATE: There's a list showing up whenever I type something on the textbox, but it has no text in it. I know it's fetching, but the word itself is not showing on the list.
Solved my problem.
Have the script like this:
<!-- WITHOUT THESE THREE BELOW, THE AUTOCOMPLETE WILL LOOK UGLY OR WILL NOT WORK AT ALL -->
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script>
$(function() {
$( "#tags" ).autocomplete({
source: "autocomplete.php"
});
});
</script>
And autocomplete.php (where we will get the data to fill the autocomplete input field):
<?php
include("conn.php"); /* ESTABLISH CONNECTION IN THIS FILE; MAKE SURE THAT IT IS mysqli_* */
$stmt = $con->prepare("SELECT description FROM table"); /* START PREPARED STATEMENT */
$stmt->execute(); /* EXECUTE THE QUERY */
$stmt->bind_result($description); /* BIND THE RESULT TO THIS VARIABLE */
while($stmt->fetch()){ /* FETCH ALL RESULTS */
$description_arr[] = $description; /* STORE EACH RESULT TO THIS VARIABLE IN ARRAY */
} /* END OF WHILE LOOP */
echo json_encode($description_arr); /* ECHO ALL THE RESULTS */
?>
这篇关于基于SQL数据库的自动完成文本框结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!