HTMLの構文など色々と修正してみました。
入力画面
HTML
1<?php
2$errorm = array();
3
4if (isset($_POST["submit"])) {
5 $name = $_POST["name"];
6 $gender = $_POST["gender"];
7 $age = $_POST["age"];
8 $message = $_POST["message"];
9
10 if ($name === '') {
11 $errorm['name'] = "名前を入力してください";
12 }
13 if ($gender === '') {
14 $errorm['gender'] = "性別にチェックを入れてください";
15 }
16 if ($age === '') {
17 $errorm['age'] = "年齢を入力してください";
18 }
19 if ($name === '') {
20 $errorm['message'] = "メッセージを入力してください";
21 }
22}
23?>
24<!DOCTYPE html>
25<html>
26<head>
27 <meta charset="utf-8">
28 <title>問い合わせフォーム!</title>
29 <link rel="stylesheet" href="index.css">
30 <style type="text/css">
31 body {
32 background-color: pink;
33 }
34
35 textarea {
36 vertical-align: top;
37 }
38 </style>
39</head>
40<body>
41<h2>問い合わせフォーム</h2>
42<form action="index.php" method="post">
43 <label>
44 名前 <input name="name" type="text">
45 </label>
46 性別
47 <label>
48 男性<input name="gender" type="radio" value="男性"/>
49 </label>
50 <label>
51 女性<input name="gender" type="radio" value=女性/>
52 </label>
53 <br>
54 <label>年齢
55 <select name="age">
56 <option value="10">10代未満</option>
57 <option value="20">20代</option>
58 <option value="30">30代</option>
59 <option value="40">40代</option>
60 <option value="50">50代</option>
61 <option value="60">60代以上</option>
62 </select>
63 </label>
64 <br>
65 <label>メッセージ(お問い合わせ内容)
66 <textarea name="message" rows="5" cols="50"></textarea>
67 </label>
68 <br>
69 <input type="submit" name="kakunin" value="確認">
70</form>
71</body>
72</html>
確認画面
HTML
1<!DOCTYPE html>
2<html>
3<head>
4 <meta charset="utf-8">
5 <title>問い合わせフォーム!</title>
6 <link rel="stylesheet" href="index.css">
7</head>
8<body>
9<h2>問い合わせ確認</h2>
10<p>こちらでお間違いありませんか?</p>
11<form action="index.php" method="post">
12 <?php
13 $name = $_POST["name"];
14 $gender = $_POST["gender"];
15 $age = $_POST["age"];
16 $message = $_POST["message"];
17
18 echo "名前" . ' ' . ' ' . $name;
19 echo "<br />";
20 echo "性別" . ' ' . ' ' . $gender;
21 echo "<br />";
22 echo "年齢" . ' ' . ' ' . $age;
23 echo "<br />";
24 echo "<br />";
25 echo "メッセージ内容" . ' ' . ' ' . $message;
26 ?>
27 <input type="hidden" name="name" value="<?php echo $name; ?>">
28 <input type="hidden" name="gender" value="<?php echo $gender; ?>">
29 <input type="hidden" name="age" value="<?php echo $age; ?>">
30 <input type="hidden" name="message" value="<?php echo $message; ?>">
31 <!--
32 意図している動作がわからなかったのでとりあえずコメントアウト。
33 < ?php
34 if (isset($_POST["name"])) != $name{
35 $errorm ['name'] = "名前が違います"
36 }
37 if (isset($_POST["gender"])) != $gender{
38 $errorm ['gender'] = "名前が違います"
39 }
40 if (isset($_POST["age"])) != $age{
41 $errorm ['age'] = "名前が違います"
42 }
43
44 if (isset($_POST["message"])) != $message{
45 $errorm ['message'] = "名前が違います"
46 }
47 ?>
48 -->
49 <br><br>
50 <input type="button" onclick="history.back()" value="戻る">
51 <input type="submit" value="完了">
52</form>
53</body>
54</html>
完了画面
HTML
1<!DOCTYPE html>
2<html>
3<head>
4</head><!-- 閉じる。 -->
5<body><!-- bodyタグの開始タグがない。 -->
6<form action="index.php" method="post">
7 <h2>お問合せありがとうございましたm(_ _)m</h2>
8 <input type="button" onclick="history.back(-2)" value="最初に戻る">
9</form>
10</body>
11</html>
追記
PHP
1<?php
2if (session_status() !== PHP_SESSION_ACTIVE) {
3 session_cache_expire(0);
4 session_cache_limiter('private_no_expire');
5 session_start();
6}
7if (!isset($_SESSION["token"])) {
8 $_SESSION["token"] = "";
9}
10
11$errorm = "";
12$state = "";
13
14function h($str)
15{
16 return htmlspecialchars($str, ENT_QUOTES, "UTF-8");
17}
18
19function getToken()
20{
21 $token = null;
22 if (is_null($token)) {
23 $token = hash("sha256", uniqid());
24 }
25 $_SESSION["token"] = $token;
26 return $token;
27}
28
29function checkToken($str)
30{
31 return (isset($_SESSION["token"]) && $_SESSION["token"] === $str);
32}
33
34foreach (["name", "gender", "age", "message"] as $value) {
35 $$value = "";
36}
37
38if (filter_input(INPUT_POST, "submit")) {
39 if (checkToken(filter_input(INPUT_POST, "token"))) {
40 $state = "submit";
41 unset($_SESSION["token"]);
42 foreach (["name", "gender", "age", "message"] as $value) {
43 if (empty(filter_input(INPUT_POST, $value))) {
44 $errorm = "未入力箇所があります。";
45 $state = "";
46 break;
47 } else {
48 $_SESSION[$value] = filter_input(INPUT_POST, $value);
49 }
50 }
51 } else {
52 $state = "";
53 foreach (["name", "gender", "age", "message"] as $value) {
54 $$value = filter_input(INPUT_POST, $value);
55 }
56 }
57} else if (filter_input(INPUT_POST, "confirm")) {
58 if (checkToken(filter_input(INPUT_POST, "token"))) {
59 $state = "confirm";
60 unset($_SESSION["token"]);
61 foreach (["name", "gender", "age", "message"] as $value) {
62 if (empty(filter_input(INPUT_POST, $value))) {
63 $errorm = "未入力箇所があります。";
64 $state = "";
65 break;
66 } else {
67 if ((!isset($_SESSION[$value])) || $_SESSION[$value] !== filter_input(INPUT_POST, $value)) {
68 $state = "";
69 }
70 }
71 }
72 } else {
73 $state = "";
74 }
75 //送られてきた内容の保存などの処理は省略。
76}
77?>
78<!DOCTYPE html>
79<html lang="ja">
80<head>
81 <meta charset="utf-8">
82 <title>タイトル</title>
83 <style type="text/css">
84 * {
85 margin: 0;
86 padding: 0;
87 }
88
89 label {
90 display: block;
91 }
92 </style>
93</head>
94<body>
95<?php if (!empty($errorm)): ?>
96 <strong><?= $errorm ?></strong>
97<?php endif; ?>
98<?php if ($state === "submit"): ?>
99 <form action="" method="post">
100 <ul>
101 <li><?= h($_SESSION["name"]) ?></li>
102 <li><?= h($_SESSION["gender"]) ?></li>
103 <li><?= h($_SESSION["age"]) ?></li>
104 <li><?= h($_SESSION["message"]) ?></li>
105 </ul>
106 <input type="hidden" name="name" value="<?= h($_SESSION["name"]) ?>">
107 <input type="hidden" name="gender" value="<?= h($_SESSION["gender"]) ?>">
108 <input type="hidden" name="age" value="<?= h($_SESSION["age"]) ?>">
109 <input type="hidden" name="message" value="<?= h($_SESSION["message"]) ?>">
110 <input type="hidden" name="token" value="<?= h(getToken()) ?>">
111 <input type="submit" name="confirm">
112 </form>
113<?php elseif ($state === "confirm"): ?>
114 <p>送信完了。</p>
115<?php else: ?>
116 <form action="" method="post">
117 <label>お名前:
118 <input type="text" name="name" value="<?= h($name) ?>">
119 </label>
120 <span>性別</span>
121 <label>男性:
122 <input type="radio" name="gender" value="男性" <?php if ($gender === "男性") echo "checked"; ?>>
123 </label>
124 <label>女性:
125 <input type="radio" name="gender" value="女性" <?php if ($gender === "女性") echo "checked"; ?>>
126 </label>
127 <label>その他:
128 <input type="radio" name="gender" value="その他" <?php if ($gender === "その他") echo "checked"; ?>>
129 </label>
130 <label>年齢:
131 <select name="age"><?= $age ?>
132 <?php foreach (["10", "20", "30", "40", "50", "60"] as $value): ?>
133 <?php if ($age === $value): ?>
134 <?php if ($value === "10"): ?>
135 <option value="<?= $value ?>" selected><?= $value ?>代未満</option>
136 <?php elseif ($value === "60"): ?>
137 <option value="<?= $value ?>" selected><?= $value ?>代以上</option>
138 <?php else: ?>
139 <option value="<?= $value ?>" selected><?= $value ?>代</option>
140 <?php endif; ?>
141 <?php else: ?>
142 <?php if ($value === "10"): ?>
143 <option value="<?= $value ?>"><?= $value ?>代未満</option>
144 <?php elseif ($value === "60"): ?>
145 <option value="<?= $value ?>"><?= $value ?>代以上</option>
146 <?php else: ?>
147 <option value="<?= $value ?>"><?= $value ?>代</option>
148 <?php endif; ?>
149 <?php endif; ?>
150 <?php endforeach; ?>
151 </select>
152 </label>
153 <label>メッセージ(お問い合わせ内容):
154 <textarea name="message" rows="5" cols="50"><?= h($message) ?></textarea>
155 </label>
156 <input type="hidden" name="token" value="<?= h(getToken()) ?>">
157 <input type="submit" name="submit">
158 </form>
159<?php endif; ?>
160</body>
161</html>
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/03/09 16:27
2017/03/10 03:19
2017/03/10 05:15
退会済みユーザー
2017/03/10 05:16
2017/03/11 15:57