我的代码有问题。
html
<div class="input-group">
<input type="text" class="form-control" id="search">
</div>
data.php
date_default_timezone_set("Asia/jakarta");
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "dbautocomplete";
$koneksi = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname)or die("error koneksi");
if(mysqli_connect_errno()){
echo 'Gagal melakukan koneksi ke Database : '.mysqli_connect_error();
}
$row_set = array();
$term = trim(strip_tags($_GET['q']));
$qstring = "SELECT * FROM tbl_auto WHERE nama_anime LIKE '".$term."%'";
$result = mysqli_query($koneksi,$qstring);
$found=mysqli_num_rows($result);
if($found>0) {
while ($row = mysqli_fetch_assoc($result)) {
$row['value']=htmlentities(stripslashes($row['nama_anime']));
$row['id']=(int)$row['id'];
$row_set[] = $row;
}
}else{
$row_set[]='Not found...';
}
echo json_encode($row_set);
Jquery
$(document).ready(function(){
/*autocomplete muncul setelah user mengetikan minimal2 karakter */
$(function() {
$( "#search" ).autocomplete({
source: "data.php",
});
});
});
我已经尝试修复此代码,但是在字段中输入一些字符后没有任何响应。 请帮助我找到问题。 我已经可以从许多网络上搜索了,但是仍然找不到根本原因。
答案 0 :(得分:0)
尝试此操作,自动完成接受参数名称term
而不是q
html
<div class="input-group">
<input type="text" class="form-control" id="search">
</div>
data.php
<?php
date_default_timezone_set("Asia/jakarta");
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "dbautocomplete";
try {
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname) or die("error connecting database");
if(mysqli_connect_errno()){
echo 'Gagal melakukan conn ke Database : '.mysqli_connect_error();
}
$term = trim(strip_tags($_GET['term']));
$qry = "SELECT * FROM nama_anime WHERE nama_anime LIKE '".$term."%'";
$query = mysqli_query($conn, $qry);
if(!$query) {
$error = mysqli_error($conn);
throw new Exception($error);
}
$result = array();
while ($row = mysqli_fetch_assoc($query)) {
$row['value'] = htmlentities(stripslashes($row['nama_anime']));
$row['id'] = $row['id'];
$result[] = $row;
}
echo json_encode($result);
} catch (Exception $e) {
$result['error'] = $e->getMessage();
echo json_encode($result);
}
mysqli_close($conn);
Jquery
$(document).ready(function(){
/*autocomplete muncul setelah user mengetikan minimal2 karakter */
$(function() {
$( "#search" ).autocomplete({
source: "data.php",
});
});
});