(0)
(0)
(0)
(0)
Total: 0 Webサービスを利用したAJAX郵便番号検索
郵便番号から住所を検索したり、住所から郵便番号を検索するTipsは様々なサイトで公開されています。
大体の場合は、郵便事業株式会社様(旧日本郵政公社)が提供しているCSVデータをDBに保持して・・という形式が多いようです。
確かに自前でDBを用意して検索アプリを作成し、郵便番号情報を常に最新にメンテナンスできる環境があれば、最善の方法です。
ただ、「そこまでは必要ないけども、もっと簡単に実装したい」という場合を想定して、WEBサービスを利用し簡単に実装するサンプルを作成しました。
今回のサンプルはこちらで確認できます。
よくある住所欄の入力フォームで
・郵便番号を入力するとオートコンプリート形式で該当住所を表示
・住所をクリックすると入力フォーム内の住所欄に値を代入する
というものです。
WEBサービスについて
郵便番号検索のWEBサービスはいくつか公開されていたのですが、グルーブテクノロジー株式会社様の仕様がわかり易く、複数の形式にも対応していますので、今回はこちらのサービスを利用させて頂きました。
仕組みの概要
リクエストの流れは以下のようになります。
クライアントjs → サーバー側PHP → WEBサービス → サーバー側PHP → クライアントjs
1.javascriptで郵便番号フィールドを定期的に監視
2.値に変化があれば、値をパラメータとしてサーバー側のPHPを経由してWEBサービスにリクエストを送信(REST)
3.WEBサービスからの結果をPHPで加工し、クライアント側へ送信
4.結果データのクリックイベントに値をフィールドに反映させる処理を追加
※WEBサービスはJSONPを使えばサーバーPHPを介さずに直接データを取得することも可能です
実装方法
HTML内でprototype.jsと今回作成したctl_auto_zip.jsを読み込み、scriptタグでajax部分の初期化を行います。
CtlAutoZipクラスの各引数は、郵便番号、都道府県、市区郡、それ以下の住所 を格納する各フィールドのエレメントIDを指定します。
-
<script type="text/javascript" src="/sample/zip_code/prototype.js"></script>
-
<script type="text/javascript" src="/sample/zip_code/ctl_auto_zip.js"></script>
-
new CtlAutoZip('zip', 'pref', 'addr1', 'addr2');
-
</script>
-
:
-
中略
-
:
-
<td valign="top">郵便番号</td>
-
<input type="text" name="zip" id="zip" value="" style="width:60px" />
-
</td>
-
</tr>
-
<td>都道府県</td>
-
<input type="text" name="pref" id="pref" value="" style="width:100px" />
-
</td>
-
</tr>
-
<td>住所1</td>
-
<input type="text" name="addr1" id="addr1" value="" style="width:300px" />
-
</td>
-
</tr>
-
<td>住所2</td>
-
<input type="text" name="addr2" id="addr2" value="" style="width:300px" />
-
</td>
-
</tr>
-
:
ソースコード
ctl_auto_zip.js
※スクリプト内のthis.server_urlの設定は次のPHPファイルのパスに変更する必要があります。
-
/***
-
* AutoZip Control
-
*/
-
var CtlAutoZip = Class.create({
-
initialize: function(zip_id, pref_id, city_id, town_id){
-
this.zip_id = zip_id;
-
this.pref_id = pref_id;
-
this.city_id = city_id;
-
this.town_id = town_id;
-
-
this.server_url = '/sample/zip_code/ajax_zip_code.php';
-
this.zip_list_id = 'auto_zip_list';
-
this.zip_value = '';
-
-
this.load();
-
},
-
-
load: function(){
-
document.observe('dom:loaded', function() {
-
// 郵便番号の初期値
-
this.zip_value = $F(this.zip_id);
-
-
// Ajaxの監視イベント設定
-
new PeriodicalExecuter(this.ajax_zip.bind(this), 0.5);
-
-
// 候補リストを閉じる
-
$(this.pref_id).observe('focus', function(event){
-
$(this.zip_list_id).hide();
-
}.bind(this));
-
-
$(this.city_id).observe('focus', function(event){
-
$(this.zip_list_id).hide();
-
}.bind(this));
-
-
$(this.town_id).observe('focus', function(event){
-
$(this.zip_list_id).hide();
-
}.bind(this));
-
-
// 候補リストのエレメントを作成
-
$(this.zip_id).insert({
-
after: '<div id="' +this.zip_list_id+ '" style="display:none;position:absolute;z-index:999;background-color:lightyellow;border:solid 1px silver;padding:2px"></div>'
-
});
-
}.bind(this));
-
},
-
-
ajax_zip: function(){
-
if(this.zip_value == $F(this.zip_id)) return;
-
this.zip_value = $F(this.zip_id);
-
-
new Ajax.Updater(
-
this.zip_list_id,
-
this.server_url,
-
{
-
"method": "get",
-
"parameters": "zip=" + this.zip_value,
-
-
onSuccess: function(request) {
-
if(request.responseText == ''){
-
$(this.zip_list_id).hide();
-
}else{
-
$(this.zip_list_id).show();
-
}
-
}.bind(this),
-
-
onComplete: function(request) {
-
// コンテナ内のリンク処理
-
$$('#' + this.zip_list_id + ' a').each(function(anchor){
-
if(anchor.identify() == 'auto_zip_credit'){
-
return;
-
}
-
anchor.observe('click', function(){
-
var text = anchor.innerHTML;
-
var a = text.split(' ');
-
if(a[0]) this.zip_value = a[0];
-
if(a[0]) $(this.zip_id).setValue(a[0]);
-
if(a[1]) $(this.pref_id).setValue(a[1]);
-
if(a[2]) $(this.city_id).setValue(a[2]);
-
if(a[3]) $(this.town_id).setValue(a[3]);
-
$(this.zip_list_id).hide();
-
}.bind(this));
-
}.bind(this));
-
}.bind(this),
-
-
onFailure: function(request) {
-
$(this.zip_list_id).hide();
-
}.bind(this),
-
-
onException: function (request) {
-
$(this.zip_list_id).hide();
-
}.bind(this)
-
}
-
);
-
}
-
});
サーバー側PHPスクリプト
ajax_zip_code.php (PHP5専用)
-
<?php
-
$base_url = 'http://groovetechnology.co.jp/ZipSearchService/v1/zipsearch?';
-
-
$zip = $_GET['zip'];
-
-
// 2バイト以下のパラメータでは実行しない
-
-
// 郵便番号検索
-
$param = 'zipcode=' .$zip;
-
}else{
-
// 住所検索
-
$param = 'word=' .$zip;
-
}
-
-
-
$xml = @simplexml_load_string($xmlstr);
-
-
echo '対象が100件以上存在しています';
-
}
-
foreach($xml->address as $row){
-
echo_html(
-
$row->zipcode,
-
$row->prefecture,
-
$row->city,
-
$row->town
-
);
-
}
-
echo_credit();
-
-
function echo_html($zip, $pref, $city, $town){
-
// 文字整形
-
'(次のビルを除く)',
-
'以下に掲載がない場合',
-
);
-
'<div><a href="javascript:void(0)">%s %s %s %s</a></div>',
-
$zip, $pref, $city, $town
-
);
-
}
-
-
function echo_credit(){
-
echo '<div style="margin-top:2px;text-align:right"><a id="auto_zip_credit" href="http://groovetechnology.co.jp/webservice/" target="_blank" style="font-size:11px;color:blue;border:0">グルーブテクノロジー Web サービス</a></div>';
-
}
-
?>
ダウンロード
今回のサンプルソースは下記からダウンロードできます。
ajax_zip_code.zip
関連するその他の記事
Comments
Leave a Reply