My Web道

WEB制作科 受講記録 …とその後も続くWEB制作に関する活動・学習記録です。

※ 当サイトはアフィリエイト広告を利用しています

5/21 [PHP] アンケートフォームの作成

f:id:sntkk3:20140228223724g:plain

  • PHP(アンケートフォームの作成)

PHP(アンケートフォームの作成)

1. アンケート送信フォーム作成

Lesson14 (p.116)
アンケートフォーム (request1.php)

<?php
$defs = array(
  'name' => '山田太郎', 'email' => 'yamada@wings.msn.to',
  'zip' => '100-0000','sex' => '男性', 'age' => '40',
  'os' => array('win', 'linux'), 'memo' => '特になし'
);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>PHP入門教室</title>
</head>
<body>
<h3>アンケート</h3>
<form method="POST" action="request2.php">
  <div class="container">
    <label for="name">名前:</label><br />
    <input type="text" id="name" name="name"
      value="<?php print($defs['name']); ?>" />
  </div>
  <div class="container">
    <label for="email">メールアドレス:</label><br />
    <input type="email" id="email" name="email"
      value="<?php print($defs['email']); ?>" />
  </div>
  <div class="container">
    <label for="zip">郵便番号:</label><br />
    <input type="text" id="zip" name="zip"
      value="<?php print($defs['zip']); ?>" />
  </div>
  <div class="container">
    性別:<br />
    <?php
    $sexes = array('男性', '女性', 'その他');
    foreach ($sexes as $sex) {
      print('<label>');
      print('<input type="radio" name="sex" value="'.$sex.'"');
      if ($sex === $defs['sex']) { print(' checked'); }
      print(' />');
      print($sex.'</label>');
    }
    ?>
  </div>
  <div class="container">
    <label for="age">年齢:</label><br />
    <select id="age" name="age">
    <?php 
      for ($i = 10; $i <= 50; $i += 10) {
        print('<option value="'. $i.'"');
        if ($i === (int)$defs['age']) { print(' selected'); }
        print('>' . $i .'代</option>');
      }
    ?>
    </select>
  </div>
  <div class="container">
    ご使用のOS:<br />
    <?php
    $oss = array('win' => 'Windows', 'mac' => 'Mac',
      'linux' => 'Linux');
    foreach ($oss as $k_os => $v_os) {
      print('<label>');
      print('<input type="checkbox" name="os[]" value="'.$k_os.'"');
      foreach ($defs['os'] as $os) {
        if ($k_os === $os) { print(' checked'); }
      }
      print(' />');
      print($v_os.'</label>');
    }
    ?>
  </div>
  <div class="container">
      <label for="memo">サイトへのご意見:</label><br />
    <textarea rows="5" cols="30" id="memo"
      name="memo"><?php print($defs['memo']); ?></textarea>
  </div>
  <input type="hidden" name="quest_num" value="XXX15204" />
  <input type="submit" value="送信" />
</form>
</body>
</html>

[実行結果]



2. ユーザー定義関数

Lesson15 (p.127)
頻出処理を関数化ファイルにまとめる (Encode.php)

<?php 
 function enc( $str, $charset = 'UTF-8') {
	return htmlspecialchars($str, ENT_QUOTES, $charset); 
 }
?>

3. 入力結果表示画面作成

Lesson15 (p.126)
アンケート結果を受け取る (request2.php)

<?php require_once 'Encode.php'; ?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>PHP入門教室</title>
<style>
dt {
 float:left;	
}
dd {
  padding-left: 8em;	
}
</style>
</head>
<body>
<h3>ご回答ありがとうございました。</h3>
<p>以下の内容で送信致しました。</p>
<dl>
<dt>名前:</dt>
<dd><?php print(enc($_POST['name'])); ?></dd>
<dt>メールアドレス:</dt>
<dd><?php print(enc($_POST['email'])); ?></dd>
<dt>郵便番号:</dt>
<dd><?php print(enc($_POST['zip'])); ?></dd>
<dt>性別:</dt>
<dd><?php print(enc($_POST['sex'])); ?></dd>
<dt>年齢</dt>
<dd><?php print(enc($_POST['age'])); ?></dd>
<dt>ご使用のOS:</dt>
<dd><?php if(isset($_POST['os']) === TRUE) {
	 print(enc(implode('、', $_POST['os']))); } ?></dd>
<dt>サイトへのご意見:</dt>
<dd><?php print(nl2br(enc($_POST['memo']))); ?></dd>
<dt>アンケート番号:</dt>
<dd><?php print(enc($_POST['quest_num'])); ?></dd>
</dl>
</body>
</html>

[実行結果]

※ 当サイトはアフィリエイト広告を利用しています