当我输入字母' a'
输出如下
A
A
A
A
A
如果我输入' ad' 它消失了
预期产量 在输入' a'
时*Adilabad
*Adoni
*Amadalavalasa
*Amalapuram
用于获取数据库表数据的php代码
<?php include_once 'db.php';
$sql = 'SELECT city_name FROM master_city';
$res = mysqli_query($con,$sql);
mysqli_close($con); ?>
我的HTML代码
<input class="input-xlarge focused"
id="emp_peraddress_city" name="emp_peraddress_city"
type="text" placeholder="city"
data-provide="typeahead" data-items="4"
data-source="<?php
echo"[";
while($row=mysqli_fetch_array($res)){
echo "'".$row["city_name"]."',";
}
echo"]"; ?>">
从数据库返回的数据的echo样本是:
['Kolhapur','Port Blair','Adilabad','Adoni','Amadalavalasa','Amalapuram','Anakapalle','Anantapur','Badepalle','Banganapalle','Bapatla','Bellampalle','Bethamcherla','Bhadrachalam','Bhainsa','Bheemunipatnam','Bhimavaram','Bhongir','Bobbili','Bodhan','Chilakaluripet','Chirala','Chittoor','Cuddapah','Devarakonda']
答案 0 :(得分:1)
您的城市名称应该是双引号,例如data-source='["city-1","city-2","city-3"]'
。
数据源应该是JSON格式,而输出是字符串,这就是为什么你只得到第一个字母。
答案 1 :(得分:0)
感谢您为获得结果所做出的贡献...... 这是我用来获得自动建议的代码。
我删除了我的php代码,并使用jquery
完成HTML部分
<input class='input-xlarge focused' id='emp_peraddress_city' name='emp_peraddress_city' type='text' data-provide='typeahead' data-items='4' data-source='cities'></br>
jquery部分
$.ajax({
url: 'add_employee/fetch_city_names.php',
method: 'POST',
success: function(response) {
var cities = response;
$('#emp_peraddress_city').typeahead({source: JSON.parse(cities)});
}
});
PHP部分:fetch_city_names.php
<?php
include_once '../db.php';
// auto suggest for city
$sql_city = 'SELECT city_name FROM master_city ORDER BY city_name';
$res_city = mysqli_query($con, $sql_city);
$data_city = array();
while ($row_city = mysqli_fetch_array($res_city)) {
$data_city[].=$row_city["city_name"];
}
$convert_city = json_encode($data_city); // converting String array to JSON
echo $convert_city;
mysqli_close($con);
?>
谢谢大家..