問合せフォ-ム

問合せフォ-ム表示 PHP ファイル
quest_form.php


63 Pear Villageに戻る

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>お問合せフォ-ム表示</title>
<link async href="css/quest.css"
 rel="stylesheet" type="text/css">
</head>
<body>
<div class="main600">
<h1>お問合せフォ-ム表示</h1>
<br>
<?php
session_start();
header(
'Content-type:text/html;charset=UTF-8'
);
if(empty($_SESSION['phrase'])){
  //直接アクセスした時
  header("Location: quest_st.php");
}
if(isset($_POST['check'])){
  $_SESSION['check'] =
    $_POST['check'];
}
if($_SESSION['phrase']
  != $_SESSION['check']){
  header
  ("Location: quest_st.php");
}
require_once
 'XML/RPC2/CachedClient.php';
// 郵便番号住所検索
require_once
 'HTML/QuickForm.php';
// フォ-ム出力
$form =
 new HTML_QuickForm('newus');
$defaultValue = array
('uname' => '',
 'email' => '',
 'come' => '',
 'postnum' => '',
 'addre' => '');
foreach
  ($defaultValue
   as $key => $value){
  if(isset($_SESSION[$key])){
    $defaultValue[$key] =
     $_SESSION[$key];
  }
}
/* 
 form name="newus" id="newus"
 フォームの method 属性の
 デフォルト値は、POST、
 action 属性のデフォルト値は
 現在のファイル名となります。
 フォームの生成
 addElementメソッドを使用
*/ 
$form->addElement
  ('header',
   'title',
   'お問合せ準備');
/* form部品の種類,name,入力欄名称
   の順に記入
*/
$form->addElement
  ('text',
	 'uname',
	 '名前:',
   array('size' => 20,
    'maxlength' => 20));
$form->addElement
  ('text',
   'email',
   'メ-ルアドレス:',
   array('size' => 24));
/*
 検証ルールの定義
 addRuleメソッドを使用
 name,エラメッセージ,検証タイプ
 の順に記入
*/	
$form->addRule
  ('email',
   'メ-ルアドレスは必須です',
   'required');
$form->addRule
  ('email',
   'メ-ルアドレスが不正です',
   'email');

$form->addRule
  ('uname',
   '名前は必須です',
   'required');
$form->addRule
  ('uname',
   '1~10文字(すべて全角時)
    で記入ください',
   'rangelength',array(1,30));
$form->addElement
 ('text',
  'postnum',
  '郵便番号:
  <br>例)2160001', 
    array
    ('size' => 9,
     'maxlength' => 7));
$form->addElement
 ('submit',
  'submit1','住所名検索');
$form->addElement
 ('textarea', 'addre',
  '住所:<br>
   全角なら<br>
   40文字以内',
  array
  ('rows'=> 4,
  'cols'=> 20,
  'maxlength' => 120,));
	
$form->addElement
 ('textarea',
  'come',
  '問合せ:<br>
   内容  <br>
   全角なら  <br>
   600文字  <br>
   以内  ',
  array
  ('rows'=> 16,
  'cols'=> 60,
  'maxlength' => 1800,));
/*
 半角で1200文字分の枠を確保する
 ここに全角600文字が入れるために
 utf-8は全角3バイト必要なので
 600×3=1800バイトを maxlength
 に指定		
*/
$form->addRule
 ('come',
  '全角文字換算で1~600字で<br>
   記入してください',
  'rangelength',array(1,1800));
$form->addRule
 ('come',
  '問合せ内容は必須です',
  'required');	
$form->addRule
 ('postnum',
  '郵便番号は必須です',
  'required');
$form->addRule
 ('postnum',
  '半角数字で記入してください',
  'regex','/^[0-9]+$/');
$form->addRule
 ('postnum',
  '半角7桁で記入ください',
  'rangelength',array(7,7));
/*
 ^が文字列の最初から、
 $が最後まで、[a-zA-Z0-9]+が
 アルファベットの小文字aからz、
 大文字AからZ、数字の0から9のみ
 からなる複数の文字列、
 を意味する。
*/
$form->addRule
 ('addre',
  '全角文字換算で1~40字で
   記入してください',
  'rangelength',array(1,120));
	
$form->addElement
 ('submit', 'submit','確認');
/*
 form部品submitの時は、name,value
 の順に記入

 フォームvalue値の設定
*/
$form->setDefaults
  ($defaultValue); 
$form->setRequiredNote
 ("*必須入力項目");
$form->applyFilter
 ('__All__',
  'htmlspecialchars');
/*
 フォームのすべての要素に
 htmlspecialcharsを適用

 フォームの検証
*/
if ($form->validate()) {
  $form->process
    ('jyunbi', false);
  }

$form->display(); 

function jyunbi(){
  if(isset($_POST['submit'])){
    $_SESSION["addre"] =
      $_POST["addre"];
    $_SESSION["postnum"] =
      $_POST["postnum"];
    $_SESSION["uname"] =
      $_POST["uname"];
    $_SESSION["email"] =
      $_POST["email"];
    $_SESSION["come"] =
      $_POST["come"];
    header(
    "Location: quest_check.php");
  }
}//function jyunbi END
if(isset($_POST['submit1'])){
//①START
  $rpc =
  XML_RPC2_CachedClient::create(
'http://yubin.senmon.net/service/xmlrpc/'
, array
  ('prefix' => 'yubin.',
   'cacheOptions' => array(
   'cacheDir' => 'tmp/',
/*
 あらかじめ cacheDirとして
 tmpフォルダを
 ¥quest
   quest_form.php
   ¥tmp
 のように作成します。
 そうすると、1回検索するごとに、
 ¥tmpのなかに
 cache_d65e95e4b22ffe7da72e47a42
 fa9f4f8_0bca1bd9981c58ddd31be00
 7e932cc4e
 のようなファイルが作成されます。
*/
    'lifetime' => 3600)
    )
  );
  try {
    $result =
     $rpc->
     fetchAddressByPostcode
     ($_POST['postnum']);
  } catch (Exception $e) {
    die('<span style="color:red">
    入力した郵便番号に該当する
    住所は検索できませんでした。
    </span>');
    }
  foreach($result as $address){
/*
 $result の内容を $address['*--*']
 に分解とりあえず処理はしない
*/
  }
  if(!isset($addess['other'])){
    $addess['other'] = "";
  }
  $address['town'] =
   str_replace
   ('以下に掲載がない場合',
    '',$address['town'],$cnt);
/*
 '以下に掲載がない場合'
 の文字列を消去
*/
  $_SESSION["addre"] =
     $address['pref']
    .$address['city']
    .$address['town']
    .$addess['other'];		
  $_SESSION["postnum"] =
    $_POST["postnum"];
  $_SESSION["uname"] =
    $_POST["uname"];
  $_SESSION["email"] =
    $_POST["email"];
  $_SESSION["come"] =
    $_POST["come"];
  header(
   "Location:"
   .$_SERVER['SCRIPT_NAME']);
}//①END
?>
</div>