我有wordpress网站。我制作了自定义表,并将值放在一行中。现在我制作了管理菜单,并在其中添加了输入字段。
<input id="location1" type="text" />
我需要在键入时预测搜索。所以,如果我存储了单词“computer”和“company”,并开始键入comp,则搜索框“results computer”和“company”下应该会出现。
我添加了jquery:
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script>
$( "#location1" ).autocomplete({
source: "ajaxurl",
minLength: 1,
select: function( event, ui ) {
log( ui.item ?
"Selected: " + ui.item.value + " aka " + ui.item.id :
"Nothing selected, input was " + this.value );
}
});
</script>
function search_ajax_callback_function() {
// Implement ajax function here
}
add_action( \'wp_ajax_search_db\', \'search_ajax_callback_function\' );
我有php:
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM locations";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
}
} else {
}
$conn->close();
那么,有人知道如何在我的搜索中添加预测吗?
最合适的回答,由SO网友:KondukterCRO 整理而成
因此,在php中需要:
$query = \'SELECT Location FROM locations\';
$result = mysqli_query($link, $query);
$rows = array();
while($r = mysqli_fetch_assoc($result)) {
$rows[] = $r[\'Location\'];
}
和jquery ui自动完成:
$("#location1").autocomplete({
source: <?php echo json_encode($rows);?>,
// this part is only if you want to force autocomplete select
change: function (event, ui) {
if(!ui.item){
$(event.target).val("");
}
},
focus: function (event, ui) {
return false;
} //end autocomplete force
});