LoginSignup
0
1

More than 5 years have passed since last update.

ajaxでphpに値を渡す

Last updated at Posted at 2015-11-26

お世話になります。

現在、郵便番号から住所を検索するというよくある機能を実装しているのですが、うまくいかなくて困っています。

郵政省からダウンロードしたCSVを必要な情報にコンバートし、php側で検索する部分まではできたのですが、javascript&htmlのフォームで入力された郵便番号をphpにajaxで渡し、コールバックされた値をフォームに自動入力されるという部分ができなくて困っています。

コンバートされたcsvから、検索するphpのコードを以下に示します

$zipcodeにajaxで渡ってきた郵便番号を入れたいということです。

どうぞ宜しくお願い致します。

<?php
//郵便番号
$zipcode = '123-4567';

$dir = __DIR__ . '/zipcode';

$zipcode = mb_convert_kana($zipcode, 'a', 'utf-8');
$zipcode = str_replace(array('-','ー'),'', $zipcode);

$result = array();

$file = $dir . DIRECTORY_SEPARATOR . substr($zipcode, 0, 1) . '.csv';
if(file_exists($file)){
    $spl = new SplFileObject($file);
    while (!$spl->eof()) {
        $columns = $spl->fgetcsv();
        if(isset($columns[0]) && $columns[0] == $zipcode){
            $result = array($columns[1], $columns[2], $columns[3]);
            break;
        }
    }
}

if(!empty($result)){
    echo $result[0] . $result[1] . $result[2];
} else {
    echo 'Not Found';
}

追記

重大なマナー違反をしてしまい大変申し訳ありません。
回答を急ぐあまりマルチポストをしていました。
以下、teratallでの類似の質問です。

0
1
6

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1