1

我正在使用 jquery-ui-autocomplete 从我的 mysql 数据库中成功检索一个字段,但是我需要从列表中获取用户的选择,从数据库中重新选择并使用从数据库中检索到的地址填写多个表单.

这是我到目前为止工作的代码,但我不知道如何继续,以获得我需要的结果。场景是这样的 - 我有一个 INPUT 字段,其中填充了从数据库中检索到的邮政编码。然后用户选择一个邮政编码,从该选择中我需要数据库中的所有地址来填充单个表单。

我的 HTML 文件:

$(document).ready(function() {
                $("#autocomplete").autocomplete({
                    autoFocus: true,
                    source: "search.php",
                    minLength: 2, //search after two characters
                    select: function(event, ui){
                        //do something
                        }
                    });
            });

<body>
    <input id="autocomplete" type="text" name="autocomplete" />
</body>

我的 search.php 文件:

// DB connection is done here

$term = trim(strip_tags($_GET['term']));//retrieve the search term that autocomplete sends

$qstring = "SELECT DISTINCT zipcode FROM table WHERE zipcode LIKE '%".$term."%' ORDER BY zipcode ASC";
$result = mysql_query($qstring); //query the database for entries containing the term

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) //loop through the retrieved values
{
$row['zipcode'] = htmlentities(stripslashes($row['zipcode']));
$row['id'] = (int)$row['id'];
$row_set[] = $row['zipcode']; //build an array
}

echo json_encode($row_set); //format the array into json data

当然,我在查询中使用 DISTINCT 来消除多个相同的邮政编码。

谢谢您的帮助!罗伯特

4

1 回答 1

0

It sounds like you want to use ajax. It will perform your search the same way it performs your autocomplete searches. The 'select' event will run whenever an autocomplete selection is made. If the user types in the full zipcode without using the autocomplete, I do not believe it will trigger. The 'change' event triggers upon leaving the field. It won't trigger until the user leaves the autocomplete field.

select: function(event, ui){

    $.ajax({
        url: "otherSearch.php",
        type: "POST",
        data: { input1 : input1,
                input2 : input2 },
        success: function(data) {

            //parse returned data and fill in the forms

            });
        }
    }
}

more info on ajax: http://api.jquery.com/jQuery.ajax/

于 2012-07-12T13:16:52.607 回答