2022年07月06日更新

自前の郵便番号辞書(SQLite)を構築して、問い合わせフォームに、郵便番号から、住所を挿入します。 よくAPIを使用する方法を紹介しているサイトを見かけますが、お金をもらっているプロが、 他人のふんどしとは、クライアントに失礼ですね。APIだと、提供元が突然サービスを中止すれば、 すべてのクライアントにご迷惑をおかけすることになります。 サービスを停止しなくても、サービスの重い日は、クライアントの動作も重くなり、トラブルの原因になりかねません。 ここでは、SQLiteを使用して、データベースを構築します。 SQLiteなら、本格的なデータベースサーバも必要なく、単独で動作しますので、使いまわしも便利です。


こんなイメージですね。郵便番号欄に7桁の郵便番号を指定して[住所検索]ボタンを押してみてください。

お問い合わせフォーム
氏名
住所

Email
いかがですか、自サイト内に設置されたデータベースから取得していますので、他のサイトや、通信状況に影響されることなく、高速に動作します。 これで、すべて完了です。 も一度、ブラウザでアクセスして、郵便番号を指定してみましょう。 正常に、すべてのフィールドに、住所が挿入されれば、完成です。
最後に、inquiry.htmlのソースです。
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>お問い合わせフォーム</title>
  <link href="/common/jquery-ui-1.13.1.custom/jquery-ui.css" type="text/css" />
  <script src="/common/jquery-ui-1.13.1.custom/external/jquery/jquery.js"></script>
  <script src="/common/jquery-ui-1.13.1.custom/jquery-ui.js"></script>
  <script type="text/javascript">
  $(document).ready(function() {
    $('input[type=button],input[type=submit],
     input[type=file], input[type=reset],button').button();
  });
  function getAjaxcity(param, path)
  {
    if (!path) path = "/zip/";
    var Param = param.split(",");
    if (!param) Param = array('mailform','Zip','Pre','City','Addr');
    var url = '';
    var zip = $('#'+Param[1]).val();
    if (!zip) {
      alert('7桁の郵便番号を指定してください。\nPlease specify the 7-digit postal code.');
      $('#'+Param[1]).focus();
      return;
    }
    url = path + 'getcity.php?zip='+zip;
    $.get(url, function(data) {
      var Address = data.split(",");
      if (Address[0] == '') {
        alert('指定の郵便番号で見つかりません。');
        $('#'+Param[1]).focus();
      } else {
        $('#'+Param[1]).val(Address[0]);
        if (!Param[3]) {
          $('#'+Param[2]).val(Address[1] + Address[2] + Address[3]);
        } else if (!Param[4]) {
          $('#'+Param[2]).val(Address[1]);
          $('#'+Param[3]).val(Address[2] + Address[3]);
        } else {
          $('#'+Param[2]).val(Address[1]);
          $('#'+Param[3]).val(Address[2]);
          $('#'+Param[4]).val(Address[3]);
        }
      }
    });
  }
  </script>
  <style type="text/css">
    #formtable {width:600px;margin:50px auto;}
    #formtable td {border-bottom:#888 dashed 1px;}
    .fieldtitle {text-align:center;}
    input[type=text],input[type=password],select,textarea {
      padding:6px;
      border: solid #aaa 1px;
      border-radius:4px;
      width:100%;
      max-width:240px;
    }
    #ziplist {width:100%;border:solid #aaa 1px;}
    #ziplist th {background:#ddd;}
    #ziplist tr.line:hover {background:#8f8;cursor:pointer;}
  </style>
</head>
<body>
  <form action="" method="post" name="mailform" id="mailform">
    <table cellspacing="0" cellpadding="10" id="formtable">
      <tr><th colspan="2" class="ui-widget-header">お問い合わせフォーム</th></tr>
      <tr><td class="fieldtitle">氏名</td>
        <td><input type="text" name="name" /></td></tr>
      <tr><td class="fieldtitle">住所</td>
        <td><p><input type="text" name="zip" id="Zip"
          placeholder="7桁数値" style="width:80px;" />
          <input type="button" value="住所検索" onclick="
            getAjaxcity('mailform,Zip,Pre,City,Addr', './');" /></p>
        <p style="margin-top:2px;"><input type="text" name="pre"
             id="Pre" placeholder="都道府県" style="width:80px;" />
          <input type="text" name="city" id="City"
            placeholder="市区町村" style="width:80px;" />
          <input type="text" name="addr" id="Addr"
            placeholder="町域以下" style="width:160px;" /></p>
        <p style="margin-top:2px;"><input type="text" name="buill"
          id="buill" placeholder="ビル、マンション名"
          style="max-width:356px;" /></p></td></tr>
      <tr><td class="fieldtitle">Email</td>
        <td><input type="text" name="email" /></td></tr>
      <tr><td class="fieldtitle">お問い合わせ</td>
          <td><textarea style="max-width:360px;height:160px;"></textarea></td></tr>
      <tr><th colspan="2" class="ui-widget-header"><input type="button"
        value="送信する" style="width:120px;margin:20px;"></th></tr>
    </table>
  </form>
</body>
</html>