douni1396 2010-08-12 20:15
浏览 24

Jquery自动完成

I have mysql table like this:

 country   | city_accented |   latitude   |    longitude |
 ---------------------------------------------------------
 australia |    sydney     | -33.8833333  |  151.2166667 |
    USA    |    dallas     |  35.3163889  |  -81.1763889 |

I'm using jquery autocomplete to get country names in form text field. How can I post with this Jquery function all other data from database (latitude, longitude, ...) to my form and use it there like hidden field ?

The Jquery code:

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>';                                        
}
}
}
  • 写回答

2条回答 默认 最新

  • dtgv52982 2010-08-12 20:21
    关注

    Rather than post the html back I would just post the json object back and build the needed html on the client side. So you can get your array back from the mysql call and submit that back:

    $return_results = array();
    
    while($row = mysql_fetch_array($result))
    {
    $return_results[] = $row;                      
    }
    
    echo json_encode($return_results);
    

    Then in the javascript code you can work with that array to do whatever you want with the data. Create the li items. Create hidden fields with data. Whatever.

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100