お問い合わせフォーム(省略)→内容確認→DB接続→「ありがとうございました」画面
contact_check.php
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>お問い合わせ確認</title>
<link href="https://fonts.googleapis.com/css?family=Amatic+SC" rel="stylesheet">
<link type="text/css" rel="stylesheet" href="./css/contact.css">
</head>
<body>
<?php
require "header.php";
?>
<main>
<?php
$name = $_POST['name'];
$kana = $_POST['kana'];
$zip = $_POST['zip'];
$addr = $_POST['addr'];
$tel = $_POST['tel'];
$email = $_POST['email'];
$company = $_POST['company'];
$department = $_POST['department'];
$text = $_POST['text'];
?>
<section class="container">
<h2 class="container-title"><span>お問い合わせ</span></h2>
<form action="contact_db_connect.php" class="form form-m h-adr" method="post">
<div class="conf">以下の内容で送信します。</div>
<li><input type="hidden" id="name-js" name="name" value="<?php echo $_POST['name']; ?>"><span>お名前</span>: <?php print $name;?></li>
<li><input type="hidden" id="hurigana-js" name="kana" value="<?php echo $_POST['kana']; ?>"><span>ふりがな</span>: <?php print $kana;?></li>
<li><input type="hidden" id="postal-js" name="zip" value="<?php echo $_POST['zip']; ?>"><span>郵便番号</span>: <?php print $zip;?></li>
<li><input type="hidden" id="address-js" name="addr" value="<?php echo $_POST['addr']; ?>"><span>住所</span>: <?php print $addr;?></li>
<li><input type="hidden" id="tel-js" name="tel" value="<?php echo $_POST['tel']; ?>"><span>電話番号</span>: <?php print $tel;?></li>
<li><input type="hidden" id="email-js" name="email" value="<?php echo $_POST['email']; ?>"><span>メールアドレス</span>: <?php print $email;?></li>
<li><input type="hidden" id="company-js" name="company" value="<?php echo $_POST['company']; ?>"><span>会社名</span>: <?php print $company;?></li>
<li><input type="hidden" id="department-js" name="department" value="<?php echo $_POST['company']; ?>"><span>部署名</span>: <?php print $department;?></li>
<li><input type="hidden" id="content-js" name="text" value="<?php echo $_POST['name']; ?>"><span>お問い合わせ内容</span>: <?php print $text;?></li>
<button class="btn btn-corp btn-l">送信</button>
</form>
</section>
</main>
<footer class="footer">
<p>Copyright © 会社名. All Rights Reserved</p>
</footer>
</body>
</html>
contact_db_connect.php
<?php
require('function.php');
if(!empty($_POST)){
$name = htmlspecialchars($_POST['name'], ENT_QUOTES);
$kana = htmlspecialchars($_POST['kana'], ENT_QUOTES);
$zip = htmlspecialchars($_POST['zip'], ENT_QUOTES);
$addr = htmlspecialchars($_POST['addr'], ENT_QUOTES);
$tel = htmlspecialchars($_POST['tel'], ENT_QUOTES);
$email = htmlspecialchars($_POST['email'], ENT_QUOTES);
$company = htmlspecialchars($_POST['company'], ENT_QUOTES);
$department = htmlspecialchars($_POST['department'], ENT_QUOTES);
$text = htmlspecialchars($_POST['text'], ENT_QUOTES);
//DBへの接続準備
$dsn = 'mysql:dbname=データベース名;host=localhost;charset=utf8';
$user = 'root';
$password = '';
$options = array(
// SQL実行失敗時にはエラーコードのみ設定
PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT,
// デフォルトフェッチモードを連想配列形式に設定
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
// バッファードクエリを使う(一度に結果セットをすべて取得し、サーバー負荷を軽減)
// SELECTで得た結果に対してもrowCountメソッドを使えるようにする
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
);
// PDOオブジェクト生成(DBへ接続)
$dbh = new PDO($dsn, $user, $password, $options);
//SQL文(クエリ作成)
$sql = 'INSERT INTO contact (name,kana,zip,addr,tel,email,company,department,text,create_date,ip) VALUES (:name,:kana,:zip,:addr,:tel,:email,:company,:department,:text,:create_date,:ip)';
$stmt = $dbh->prepare($sql);
//プレースホルダーに値をセットし、SQL文を作成
$stmt->execute(array(':name' => $name, ':kana' => $kana, ':zip' => $zip, ':addr' => $addr, ':tel' => $tel, ':email' => $email, ':company' => $company, ':department' => $department, ':text' => $text, ':create_date' => date('Y-m-d H:i:s'), ':ip' => null));
//DBに登録後はthanks.phpへ遷移
header("Location:thanks.php");
};
?>