ユーザが選択したオプションに基づいて、mycodeデータベースのデータでポストコード(zipcode)入力フィールドにデータを入力しようとしています。 jQueryオートコンプリートフィールドの郊外。JqueryオートコンプリートとPHP:オートコンプリートフィールドで選択されたオプションに基づいて、入力フィールドにmySQLデータベースのデータを入力する
オートコンプリートはうまく機能します。フィルタされたSuburbsリストは、ユーザーからの入力条件に基づいて取得されます。ソース参照はPHPファイルです。しかし、ユーザーの選択したオプションを使用してデータベースにコールバックして郵便番号を取得する方法を理解することはできません。おそらく、郊外と同時に、最初の呼び出しで郵便番号を取得することができます。私はすべての郵便番号を望んでいませんが、ユーザーが選択を終了するだけです。次のように
私のjQueryのは、次のとおりです( "$( '#の郵便番号')" ラインは、まだ動作しません...)
<script type="text/javascript" src="js/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.15.custom.min.js"></script>
<script>
// autocomplete
$(function() {
$("#suburbs").autocomplete({
source: "allSuburbs.php",
minLength: 3,
select: function(event, ui) {
$('#postcodes').val(ui.item.postcode);
},
});
});
</script>
関連のhtml:
<p>Suburb</p><input class="inputText" type="text"
size="50" name="term" id="suburbs" maxlength="60" /></td>
<td><p>State</p><input class="inputText" type="text"
size="5" name="" id="states" maxlength="4" /></td>
<td><p>Postcode</p><input class="inputText" type="text"
size="5" name="" id="postcodes" maxlength="4" /></td>
PHP (allSuburbs.php):
:<?php
$con = mysql_connect("***","***","***");
if (!$con) { die('Could not connect: ' . mysql_error()); }
$dbname = 'suburb_state';
mysql_select_db($dbname);
$query = "SELECT name FROM suburbs";
$result = mysql_query($query);
if (!$result) die ("Database access failed:" . mysql_error());
//retrieving the search term that autocomplete sends
$qstring = "SELECT name FROM suburbs WHERE name LIKE '%".$term."%'";
//query the database for entries containing the term
$result = mysql_query($qstring);
//loop through the retrieved values
while ($row = mysql_fetch_array($result,MYSQL_ASSOC))
{ $row['name']=htmlentities(stripslashes($row['name']));
$row['postcode']=htmlentities(stripslashes($row['postcode']));
$row_set[] = $row['name'];//build an array
}
echo json_encode($row_set);//format the array into json data
mysql_close($con);
?>
私はおそらく最も有用thseのリンクを見つけました
http://www.simonbattersby.com/blog/jquery-ui-autocomplete-with-a-remote-database-and-php/ (これは、最初に私を助けた)
http://www.jensbits.com/2010/05/29/using-jquery-autocomplete-to-populate-another-autocomplete-asp-net-coldfusion-and-php-examples/それは郵便番号または郵便番号フィールドジップコードの範囲と状態の選択に基づいてではなく、に基づいて単一の郵便番号を移入が、(これは、私の問題に最も近いです1つの郊外/都市)。
助けてください。 ありがとう、 Andrew
ありがとう..まだまだ私のことを頭に浮かべて.. –
重要なことは、あなたのAJAXのオートコンプリート*呼び出しは 'id'と' value'を返さなければなりませんが、それらの属性だけではありません。私は他のフォーム要素を変更するために、 'state'と' postcode'も返しています。 – RET