【授業】PHPでフォーム まとめ

苦手なPHPのフォーム入力と入力データの受け取り

入力フォーム

<!DOCTYPE html>
<html lang="ja">
<meta charset="UTF-8">
<head>

<title>フォーム</title>
<style>
section {
	text-align: center;
}
table {
	width: 450px;
	margin: auto;
}
th {
	text-align: right;
}
td {
	text-align: left;
}
</style>
</head>
<body>
<section>
<form action="ans.php" method="POST"> 
<table>
<tr>
<th>氏名</th>
<td><input type="text" name="name" value="name"></td>
</tr>
<tr>	
<th>アドレス</th>
<td><input type="text" name="mail" value="mailaddress"></td>
</tr>
<tr>
<th>性別</th>
<td><input type="radio" name="gender" value="男性"><label for="male">男性</label><br>
<input type="radio" name="gender" value="女性"><label for="female">女性</label></td>
</tr>
<tr>
<th>好きな色</th>
<td>
<select name="color">
<option value="">選んでください</option>
<option value="赤"></option>
<option value="青"></option>
<option value="緑"></option>
<option value="黄"></option>
</select>
</td>
</tr>
<tr>
<th>関心があることをいくつでも選んでください。</th>
<td>
<input name="hobbies[]" type="checkbox" value="読書">読書<br>
<input name="hobbies[]" type="checkbox" value="旅行">旅行<br>
<input name="hobbies[]" type="checkbox" value="スポーツ">スポーツ
</td>
</tr>
<tr>
<th>お問い合わせ</th>
<td><textarea name="comment" cols="30" rows="3"></textarea></td>
</tr>
</table>
<input type="submit" value="送信">

</form> 
</section>
</body>
</html>

受け取り画面 :ans.php

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>受け取りフォーム</title>
<link href="style.css" rel="stylesheet" type="text/css">
<style>
section {
	text-align: center;
}
table {
	width: 400px;
	margin: auto;
}
th {
	text-align: left;
}
td {
	text-align: left;
}
</style>
</head>
<body>
<section>
<P>情報の入力ありがとうございます</P>
<table>
  <tr>
    <th>名前</th><td><?php echo( htmlspecialchars($_POST["name"], ENT_QUOTES)); ?></td>
  </tr>
  <tr>
    <th>メールアドレス</th><td><?php echo (htmlspecialchars($_POST["mail"], ENT_QUOTES)); ?></td>
  </tr>
   <tr>
    <th>性別</th><td><?php echo (htmlspecialchars($_POST["gender"], ENT_QUOTES)); ?></td>
  </tr>
  <tr>
  <th>好きな色</th><td><?php echo(htmlspecialchars($_POST["color"],ENT_QUOTES));?></td>
  </tr>
   <tr>
   <th>趣味</th><td><?php echo(htmlspecialchars(implode("、",$_POST["hobbies"]), ENT_QUOTES)); ?></td>
  </tr>
  <tr>
    <th>コメント</th><td><?php echo nl2br(htmlspecialchars($_POST["comment"], ENT_QUOTES)) ?></td>
  </tr>
</table>
</section>
</body>
</html>