我有mysql表是这样的:jQuery的自动完成
country | city_accented | latitude | longitude |
---------------------------------------------------------
australia | sydney | -33.8833333 | 151.2166667 |
USA | dallas | 35.3163889 | -81.1763889 |
我使用jQuery自动完成获得在表单文本字段国名。 如何使用这个jquery函数发布数据库中的所有其他数据(经度,纬度,...)到我的表单并像隐藏字段那样使用它?
jQuery代码:
function lookup(inputString) {
if(inputString.length == 0) {
// Hide the suggestion box.
$('#suggestions').hide();
} else {
$.post("rpc.php", {queryString: ""+inputString+""}, function(data){
if(data.length >0) {
$('#suggestions').show();
$('#autoSuggestionsList').html(data);
}
});
}
} // lookup
function fill(thisValue) {
$('#inputString').val(thisValue);
$('#suggestions').hide();
}
HTML:
<input size="30" id="inputString" onkeyup="lookup(this.value);" type="text" />
<div class="suggestionsBox" id="suggestions" style="display: none;">
<div class="suggestionList" id="autoSuggestionsList"></div>
PHP:
if(isset($_POST['queryString'])) {
$queryString = $_POST['queryString'];
if(strlen($queryString) >0) {
$query = "SELECT * FROM cities WHERE city_accented LIKE '$queryString%' LIMIT 10";
$result = mysql_query($query) or die("There is an error in database");
while($row = mysql_fetch_array($result)){
echo '<li onClick="fill(\''.$row['city_accented'].'\');">'.$row['city_accented'].','.$row['country'].' </li>';
}
}
}