読者です 読者をやめる 読者になる 読者になる

コントローラクラス

<?php
public function inputAction()
{
    $this->view->zip_search_action = $this->_helper->url('zip', 'Ajax');
}

input.tpl

            <tr>
                <th>郵便番号</th>
                <td>
                    <input type="text" id="user_zip" name="user_zip" maxlength="8" value="{$item['user_zip']|zip_format}">
                    <input type="button" id="btnSearchAddressByZip" name="btnSearchAddressByZip" value="住所検索" onClick="searchZipAddress('user_zip', 'user_address_text', '{$zip_search_action}');">
                </td>
            </tr>
            <tr>
                <th>住所</th>
                <td>
                    <input type="text" id="user_address_text" name="user_address_text" maxlength="120" value="{$item['user_address_text']}">
                </td>
            </tr>

固定javascript

function searchZipAddress(inputTarget, destTarget, action) {
    var inputID = '#' + inputTarget;
    var destID = '#' + destTarget;
    var inputVal = $(inputID).val();
    var reqURL = action + '/zip/' + inputVal +  '/';

    // inputのvalueに対してloadする
    $.get(reqURL, function(result) {
        $(destID).val(result);
    });
}

ajaxコントローラ

<?php
class AjaxController extends Zend_Controller_Action_Mybase
{
    protected function init_controller()
    {
        // 単純な文字列を返すレイアウトベース
        $this->_helper->layout->setLayout('layout_ajax');
    }

    /**
     * 郵便番号検索
     */
    public function zipAction()
    {
        $param_zip = $this->_getParam('zip');
        $address[] = array();
        $address['zip'] = AddressHelper::FormatZip($param_zip);
        $address['pref'] = '';
        $address['ciry'] = '';
        $address['area'] = '';
        $address['jigyousho_name'] = '';
        $address['message'] = '(該当する住所が見つかりません)';

        if ((!empty($param_zip)) && (AddressHelper::isZipFormat($param_zip))) {
            $sc = dataAccessService::createCriteria('zip');
            $sc->setZipCode(AddressHelper::UnFormatZip($param_zip));
            $row = dataAccessService::selectMasterZipRow($sc);

            if (count($row) > 0) {
                $address['pref'] = $row['pref'];
                $address['ciry'] = $row['city'];
                $address['area'] = $row['area'];
                $address['jigyousho_name'] = $row['jigyousho_name'];
                $address['message'] = '';
            }
        }
        
        $this->view->result = $address;
    }
}

zip.tpl

{strip}
{if $result['message'] == ''}
    {$result['pref']}{$result['city']}{$result['area']}{if $result['jigyousho_name'] != ''} {$result['jigyousho_name']}{/if}
{else}
    {$result['message']}
{/if}
{/strip}

layout_ajax.tpl

{$content nofilter}