PHP 課題23(前のページに戻っても入力した内容が消えない)

■実行結果
![課題23err入力画面.png](https://qiita-image-store.s3.amazonaws.com/0/95187/28baadba-a904-7f7d-519d-735b12ba1b60.png)
![課題23完了画面.png](https://qiita-image-store.s3.amazonaws.com/0/95187/0b1edd65-7318-2733-e615-dab499e1d07b.png)

■kadai_23_input.php

```php
<!-- http://localhost/test/kadai_23_input.php -->
<?php
session_start ();
if (isset ( $_SESSION ['err'] )) {
	$err = $_SESSION ['err'];
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xm1ns="http://www.w3.org/1999/xhtml">
<html lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Script-Type" content="text/javascript">


	<title>入力フォーム</title>
	<style type="text/css">
p#submit_btn {
	text-align: center;
}

.content {
	margin-left: 107px;
	margin-top: 0px;
}

.form {
	color: white;
	background-color: #4682B4;
	text-align: center;
	/* 	padding-left: 370px; */
	padding-top: 5px;
	padding-bottom: 5px;
	font-weight: bold;
	font-size: 95%;
	width: 455px;
}
</style>

</head>
<body>
	<h1>
		<font color="#4682B4">課題23</font>
	</h1>
	<p class="form">入力フォーム</p>

	<form action="kadai_23_output.php" method="post">
		<p>
			氏名：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input
				type="text" name="name"
				value=<?php
				if (isset ( $_SESSION ['name'] )) {
					echo $_SESSION ['name'];
				}
				?>>
					<?php
					if (isset ( $err ['name'] )) {
						echo '<p class="content"><b><font color="red">' . $err ['name'] . '</font></b></p>';
					}
					?>




		</p>
		<p>
			年齢：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			<input type="text" name="age"
				value=<?php
				if (isset ( $_SESSION ['age'] )) {

					echo $_SESSION ['age'];
				}
				?>><?php
				if (isset ( $err ['age'] )) {
					echo '<p class="content"><b><font color="red">' . $err ['age'] . '</font></b></p>';
				}
				?>




		</p>
		<p>
			性別：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			<input type="radio" name="gender" value="man"
				<?php
				// もし$_SESSION['gender']があって$_SESSION ['gender']の中の値が'男性'と等しいもしくは
				// $_SESSION ['gender']がない場合(デフォルトは男性にしとく)は男性がcheckedでチェックされる
				if (isset ( $_SESSION ['gender'] ) && $_SESSION ['gender'] == '男性' || ! isset ( $_SESSION ['gender'] )) {
					echo 'checked';
				}
				?>></input>男性 <input type="radio" name="gender" value="woman"
				<?php if(isset($_SESSION['gender']) && $_SESSION['gender'] == '女性') { echo 'checked';}?>>女性






		</p>
		<p>
			地域：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;


			<select name="region">
				<?php

				$region = array (
						"0001" => "北海道",
						"0002" => "東北",
						"0003" => "関東",
						"0004" => "中部",
						"0005" => "近畿",
						"0006" => "中国",
						"0007" => "四国",
						"0008" => "九州"
				);
				// 「"」→変数は展開される 「'」→変数は展開されない
				foreach ( $region as $key => $value ) {
					if (isset ( $_SESSION ['region'] ) && $_SESSION ['region'] == $value) {
						echo '<option value="', $key, '"selected>', $value, '</option>';
					} else {
						echo '<option value="', $key, '">', $value, '</option>';
					}
				}
				// }
				?></select>
		</p>
		<p>
			<?php
			$hobby = array (
					"旅行",
					"外食",
					"レジャー",
					"音楽鑑賞",
					"ギャンブル",
					"インターネット"
			);
			// こうかけば複数送信できる

			echo "趣味：" . "&nbsp;&nbsp;&nbsp;&nbsp;";

			foreach ( $hobby as $value ) {
				// in_array→特定の値が配列に含まれているか調べる
				// $_SESSION['hobby']の値と$valueが等しかったら
				// もし$_SESSION['hobby]が存在していて$_SESSION['hobby']配列の中に$valueの文字があったら
				if (isset ( $_SESSION ['hobby'] ) && in_array ( $value, $_SESSION ['hobby'] )) {

					echo '<input type="checkbox" name="hobby[]" value="', $value, '"checked>', "$value.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
				} else {
					echo '<input type="checkbox" name="hobby[]" value="', $value, '">', "$value.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
				}
			}

			?>
				<?php
				if (isset ( $err ['hobby'] )) {
					echo '<br/><b><font color="red">', $err ['hobby'], '</font></b>';
				}
				?>
			<?php
			// //hobby[]と書くと自動採番が付与されて↓こうなる
			// $hobby = array (
			// 0 => "レジャー",
			// 1 => "音楽鑑賞"
			// );
			//			?>
			</p>
		<!-- 			<p> -->
		<!-- 				趣味：<br><input type="checkbox" name="hobby" value="旅行">旅行&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -->
		<!-- 						<input type="checkbox" name="hobby" value="外食">外食 <input</br> -->
		<!-- 			</p> -->
		<p>
			なにか一言:<br />
			<textarea rows="6" cols="65" name="word"><?php  if(isset($_SESSION['word'])){ echo $_SESSION['word'];}?></textarea>
				<?php
				if (isset ( $err ['word'] )) {
					echo '<br/><b><font color="red">' . $err ['word'] . '</font></b>';
				}
				?>
			</p>
		<p>
			<span style="margin-left: 250px;"> <input type="submit" value="送信" /></span>
		</p>

	</form>

</body>
</html>
<?php

// var_dump ( $region );
// var_dump ( $_SESSION ['region'] );
// var_dump ( $hobby );
// var_dump ( $_SESSION ['hobby'] );
// var_dump ( array_values($_SESSION['hobby']));
?>
```

■kadai_23_output.php

```php
<!-- http://localhost/test/kadai_23_output.php -->
<?php
session_start ();

if ($_POST) {
	$name = $_POST ['name'];
	$age = $_POST ['age'];
	$gender = $_POST ['gender'];
	// var_dump ( $gender );
	if ($gender == 'man') {

		$gender = '男性';
	} else if ($gender == 'woman') {
		$gender = '女性';
	}
	// var_dump ( $gender );
	$region = $_POST ['region'];
	switch ($region) {
		case "0001" :
			$region = '北海道';
			break;
		case "0002" :
			$region = '東北';
			break;
		case "0003" :
			$region = '関東';
			break;
		case "0004" :
			$region = '中部';
			break;
		case "0005" :
			$region = '近畿';
			break;
		case "0006" :
			$region = '中国';
			break;
		case "0007" :
			$region = '四国';
			break;
		case "0008" :
			$region = '九州';
			break;
	}
	$hobby = $_POST ['hobby'];
	// var_dump ( $hobby );
	$word = $_POST ['word'];
	$message = '';
	// count関数→変数に含まれる全ての要素、or オブジェクトに含まれる何かの数を数える
	// 他の人からみて解りやすいように、$hobby>1よりも$hobby>=2の方がいい
	// もし$hobbyに格納されている要素が2以上なら
	if (count ( $hobby ) >= 2) {
		$message = '多趣味ですね';
	}

	$_SESSION ['name'] = $name;
	$_SESSION ['age'] = $age;
	$_SESSION ['gender'] = $gender;

	$_SESSION ['region'] = $region;
	$_SESSION ['hobby'] = $hobby;
	$_SESSION ['word'] = $word;

	$err = array ();

	if (strlen ( $_POST ['name'] ) == 0) {
		$err ['name'] = '氏名を入力してください。';
		$flag = false;
	}

	if (strlen ( $_POST ['age'] ) == 0) {
		$err ['age'] = '年齢を入力してください。';
		$flag = false;
	}

	if (count ( $_POST ['hobby'] ) == 0) {
		$err ['hobby'] = '趣味を選択してください。';
		$flag = false;
	}

	if (strlen ( $_POST ['word'] ) == 0) {
		$err ['word'] = '一言入力してください。';
		$flag = false;
	}
	// もし$errに格納されている要素が1以上なら
	if (count ( $err ) >= 1) {
		$_SESSION ['err'] = $err;
		header ( 'Location:kadai_23_input.php' );
	}
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xm1ns="http://www.w3.org/1999/xhtml">
<html lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Script-Type" content="text/javascript">


	<title>確認画面</title>
	<style type="text/css">
.form {
	color: white;
	background-color: #4682B4;
	text-align: center;
	/* 	padding-left: 370px; */
	padding-top: 5px;
	padding-bottom: 5px;
	font-weight: bold;
	font-size: 95%;
	width: 455px;
}
</style>

</head>
<body>
	<h1>
		<font color="#4682B4">課題23</font>
	</h1>
	<p class="form">確認画面</p>
	<p>氏名：<?php echo $_SESSION['name'];?></p>
	<p>年齢：<?php echo $_SESSION['age'];?></p>
	<p>性別：<?php echo $_SESSION['gender'];?></p>
	<p>地域：<?php echo $_SESSION['region'];?></p>
	<?php
	// 波括弧(カーリーブラケット)「{}」で囲んで変数を明示的に表す。「{}」で囲むと変数の中身が表示される
	// 配列要素を使う場合は必ず波括弧を使わなければならない
	echo '趣味：';
	foreach ( $hobby as $value ) {
		echo $value . " ";
	}
	echo '<br/>';

	?>



	<b><font color="#4682B4"><?php echo $message; ?></font></b>
	<p>何か一言：<?php echo $_SESSION['word'];?></p>

</body>
</html>
<?php

// var_dump ( $region );
// var_dump ( $_POST );
// var_dump ( $_POST ['hobby'] );
// var_dump ( $_POST ['region'] );
?>
```