お問い合わせフォーム(1)



input.php

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>練習用フォーム</title>
<style>
table {
	border-collapse: collapse;
}
</style>
</head>
<body>
<h1>入力画面</h1>
<p>必要事項を入力して「確認する」ボタンをクリックしてください。</p>
<!-- formタグ -->
<form action="check.php" method="post">
<table border="1" width="450">
<tr>
<td>お名前</td>
<!-- テキストボックス -->
<td><input type="text" name="name" size="30"></td>
</tr>
<tr>
<td>メールアドレス</td>
<td><input type="text" name="email" size="30"></td>
</tr>
<tr>
<td>メッセージ</td>
<td>
<!-- テキストエリア -->
<textarea rows="5" cols="25" name="message"></textarea>
</td>
</tr>
<tr>
<td colspan="2">
<!-- 送信ボタン -->
<input type="submit" name="sub1" value="確認する">
</td>
</tr>
</table>
</form>
</body>
</html>



check.php

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>練習用フォーム</title>
<style>
table {
	border-collapse: collapse;
}
</style>
</head>
<body>
<h1>確認画面</h1>
<p>内容を確認してください。</p>
<?php
// 入力値の取得
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
?>
<form action="submit.php" method="post">
<table border="1" width="450">
<tr>
<td>お名前</td>
<!-- 入力内容の確認表示 -->
<td><?php echo $name; ?></td>
</tr>
<tr>
<td>メールアドレス</td>
<td><?php echo $email; ?></td>
</tr>
<tr>
<td>メッセージ</td>
<!-- メッセージの改行 -->
<td><?php echo nl2br($message); ?></td>
</tr>
<tr>
<td colspan="2">
  <input type="submit" name="sub1" value="送信する">
</td>
</tr>
</table>
<!-- hiddenフィールド -->
<input type="hidden" name="name" value="<?php echo $name; ?>">
<input type="hidden" name="email" value="<?php echo $email; ?>">
<input type="hidden" name="message" value="<?php echo $message; ?>">
</form>
</body>
</html>



submit.php

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>練習用フォーム</title>
<style>
table {
	border-collapse: collapse;
}
</style>
</head>
<body>
<h1>完了画面</h1>
<?php
// 入力値を取得する
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];

// メール本文の組み立て
$to = "xxx@gmail.com";
$title = "【メールフォームより】";
$ext_header = "From:{$email}";

// 本文を組み立てるヒヤドキュメント
$body =  <<<EOM
--------------------------------------------------
【Webサイトからのメール】

お名前:{$name}
メールアドレス:{$email}
メッセージ:{$message}
--------------------------------------------------
EOM;
?>
<!-- 組み立てた内容の表示 -->
<p>メールを送信しました。</p>
<table border="1" width="450">
<tr>
<td>お名前</td>
<td><?php echo $name; ?></td>
</tr>
<tr>
<td>メールアドレス</td>
<td><?php echo $email; ?></td>
</tr>
<tr>
<td>メッセージ</td>
<td><?php echo nl2br($message); ?></td>
</tr>
</table>
</body>
</html>