<?php
//初期化
$show_form = "";
$cnt_msg = "";
$msg = "";
//設定ファイル読み込み
require 'set/setting.php';

$mode = $_POST["mode"];
if($mode == "search"){
    $select_capital = $_POST["capital"];
    $select_shi = $_POST["shi"];
    $select_cho = $_POST["cho"];

    if($select_capital == 0){
        $msg = <<< _ALERT_
    <script type='text/javascript'>;
    <!--//\n
    alert('{$alert_msg}');
    //-->
    </script>

_ALERT_;
    //都道府県選択
    }elseif($select_capital != 0 && $select_shi == NULL && $select_cho == NULL){
        $str_sql = "SELECT * FROM `zipcode` WHERE `cap_id`='{$select_capital}';";
        require_once 'set/sql.php';
    //都道府県選択、市あり
    }elseif($select_capital != 0 && $select_shi != NULL && $select_cho == NULL){
        $str_sql = "SELECT * FROM `zipcode` WHERE `cap_id`='{$select_capital}' AND `shi`LIKE'%{$select_shi}%';";
        require_once 'set/sql.php';
    //都道府県選択、市あり、町あり
    }elseif($select_capital != 0 && $select_shi != NULL && $select_cho != NULL){
        $str_sql = "SELECT * FROM `zipcode` WHERE `cap_id`='{$select_capital}' AND `shi`LIKE'%{$select_shi}%' AND `cho`LIKE'%{$select_cho}%';";
        require_once 'set/sql.php';
    //都道府県選択、町あり
    }elseif($select_capital != 0 && $select_shi == NULL && $select_cho != NULL){
        $str_sql = "SELECT * FROM `zipcode` WHERE `cap_id`='{$select_capital}' AND `cho`LIKE'%{$select_cho}%';";
        require_once 'set/sql.php';
    }

    //県名取得
    $address_option_flg = 1; 
    require 'set/set_address_option.php';

    if($select_capital == 0){
        $cap_name = "なし";
    }
    if($select_shi == NULL){
        $select_shi = "なし";
    }
    if($select_cho == NULL){
        $select_cho = "なし";
    }
    $input_txt = "｛ 入力文字 => 県名：<strong>{$cap_name}</strong>　市名：<strong>{$select_shi}</strong>　町名：<strong>{$select_cho}</strong> ｝\n";
}

//セレクトボックス生成
$address_option_flg = 2; 
require 'set/set_address_option.php';

//入力フォーム・表示フォーム
$show_form .= <<< _SHOW_FORM_
    <span class='section_red'>＊</span>は必須。
    <form method='POST'>
        <p><span class='section_red'>＊</span>県名：
            <select name='capital'>
                {$cap_option}
            </select>
        </p>
        <p>
            市名：
            <input type='text' name='shi' value=''>
        </p>
        <p>
            町名：
            <input type='text' name='cho' value=''>
        </p>
        <input type='hidden' name='mode' value='search'>
        <input type='submit' value='検索'>
    </form>

_SHOW_FORM_;

    $show_resault = <<< _SHOW_RESAULT_
    <h2>{$msg}</h2>
    <p>郵便番号候補：{$cnt_msg}<br />
    {$input_txt}</p>
    <textarea cols='50' rows='10'>{$search_rs}</textarea>

_SHOW_RESAULT_;

//ページ表示（テンプレート読み込み）
require_once 'template/template.php';

?>
