Webデザイナーへの道しるべ | suge1040's diary

Webデザイナーになる事を目指しフェリカテクニカルアカデミーで勉強していましたsuge1040によるブログです。

問い合わせフォームの作成 -PHP-

今日の授業で問い合わせフォームの作成 しました。

まず、xampp→htdocs→phpの中にmailフォルダを作成し

 

index.php

check.php

thanks.php

init.php

 

を作る。

 

 

-----index.php----

<?php

require_once './init.php';

?>

 

<!DOCTYPE html>

<html lang="ja">

<head>

<meta charset="UTF-8">

<title>お問い合わせ入力フォーム</title>

<link rel="stylesheet" href="css/style1.css">

</head>

<body>

<div id="container">

<h1>お問い合わせ入力フォーム</h1>

<form action="check.php" method="post" id="mailform">

<table>

<tr>

<th><label for="name">お名前:&nbsp;&nbsp;<span>※必須</span>&nbsp;&nbsp;30文字以内</label></th>

<td><input type="text" name="name" id="name" size="30" placeholder="例:東京太郎" ></td>

</tr>

<tr>

<th><label for="email">email:&nbsp;&nbsp;<span>※必須</span></label></th>

<td><input type="text" name="email" id="email" size="30" placeholder="例:sample@sample.com" ></td>

</tr>

<tr>

<th><label for="tel">お電話番号:&nbsp;&nbsp;<span>※必須</span>&nbsp;&nbsp;半角数字</label></th>

<td><input type="text" name="tel" id="tel" size="30" placeholder="例:123-4567-8910" ></td>

</tr>

<tr>

<th><label for="message">お問い合わせ:&nbsp;&nbsp;<span>※必須</span>&nbsp;&nbsp;128文字以内</label></th>

<td><textarea name="message" id="message" cols="30" rows="5" placeholder="例:御社商品の資料請求を致します。"></textarea></td>

</tr>

</table>

<input type="submit" value="確認">

</form>

</div>

</body>

</html>

 

<?php

ob_end_flush();

 

?>



-----check.php----

<?php

require_once './init.php';

//var_dump($_POST);

$name = $_POST['name'];

$email = $_POST['email'];

$tel = $_POST['tel'];

$message = $_POST['message'];

 

$_SESSION['mail_data'] ['name'] = $name;

$_SESSION['mail_data'] ['email'] = $email;

$_SESSION['mail_data'] ['tel'] = $tel;

$_SESSION['mail_data'] ['message'] = $message;

//var_dump($_SESSION['mail_data'] ['name']);

 

?>

 

<!DOCTYPE html>

<html lang="ja">

<head>

<meta charset="UTF-8">

<title>お問い合わせ確認画面</title>

<link rel="stylesheet" href="css/style1.css">

</head>

<body>

<div id="container">

<h1>お問い合わせ確認</h1>

<table>

<tr>

<th>お名前:</th><td><?php echo h($name); ?></td>

</tr>

<tr>

<th>email:</th><td><?php echo h($email); ?></td>

</tr>

<tr>

<th>お電話番号:</th><td><?php echo h($tel); ?></td>

</tr>

<tr>

<th>お問い合わせ内容:</th><td><?php echo h($message); ?></td>

</tr>

</table>

<p><a href="index.php">戻る</a><a href="thanks.php">送信</a></p>

</div>

</body>

</html>

<?php

ob_end_flush();

 

?>

 

-----thanks.php----

<?php

require_once './init.php';

//var_dump($_SESSION);

$name = $_SESSION['mail_data']['name'];

$email = $_SESSION['mail_data']['email'];

$tel = $_SESSION['mail_data']['tel'];

$message = $_SESSION['mail_data']['message'];

 

?>

 

<!DOCTYPE html>

<html lang="ja">

<head>

<meta charset="UTF-8">

<title>送信完了</title>

<link rel="stylesheet" href="css/style.css">

</head>

<body>

<div id="container">

<h1>送信完了</h1>

<p></p>

<table>

<tr>

<th>お名前:</th>

<td><?php echo h($name); ?></td>

</tr>

<tr>

<th>email:</th>

<td><?php echo ($email); ?></td>

</tr>

<tr>

<th>お電話番号:</th>

<td><?php echo ($tel); ?></td>

</tr>

<tr>

<th>お問い合わせ内容:</th>

<td><?php echo ($message); ?></td>

</tr>

</table>

<p><a href="index.php">戻る</a></p>

</div>

</body>

</html>

<?php

ob_end_flush();

 

?>



-----init.php----

<?php

function h($str){

 return htmlspecialchars($str,ENT_QUOTES,"UTF-8");

}

session_start();

ob_start();