phpとデータベースの連動
コンタクトフォーム
index.php
<!DOCTYPE HTML> <html lang="ja"> <head> <meta charset="utf-8"> <title>phpメールフォーム</title> <!--[if lt IE 9]> <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <!--[if lt IE 9]> <script src="http://cdnjs.cloudflare.com/ajax/libs/modernizr/2.5.3/modernizr.min.js"></script> <![endif]--> <style> article,aside,dialog,figure,footer,header,hgroup,menu,nav,section{display:block} </style> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <div class="wrapper"> <h1>お問い合わせメールフォーム</h1> <p>このフォームはphpの練習で作成したサンプルです。</p> <p>入力したメールアドレス宛に自動送信される機能がついています。</p> <p>よろしければお試しください。</p> <form action="check.php" method="post"> <table> <tr> <th><label for="name">お名前</label></th> <td><input type="text" name="name" id="name" size="30" autofocus required></td> </tr> <tr> <th><label for="email">メールアドレス</label></th> <td><input type="email" name="email" id="email" size="30" required></td> </tr> <tr> <th><label for="message">お問い合わせ内容</label></th> <td><textarea name="message" id="message" cols="30" rows="5" required></textarea></td> </tr> </table> <input type="submit" value="確認画面へ"> </form> </div> </body> </html>
確認画面 chech.php
<!DOCTYPE HTML> <html lang="ja"> <head> <meta charset="utf-8"> <title></title> <!--[if lt IE 9]> <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <!--[if lt IE 9]> <script src="http://cdnjs.cloudflare.com/ajax/libs/modernizr/2.5.3/modernizr.min.js"></script> <![endif]--> <style> article,aside,dialog,figure,footer,header,hgroup,menu,nav,section{display:block} </style> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <h1>確認画面</h1> <p>入力内容を確認してください。</p> <?php $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; print '<ul>'."\n"; print '<li>'; print 'お名前: '.$name.'様'; print '</li>'."\n"; print '<li>'; print 'メールアドレス: '.$email; print '</li>'."\n"; print '<li>'; print 'お問い合わせ内容: '.$message; print '</li>'."\n"; print '</ul>'."\n"; print '<form action="thanks.php" method="post">'."\n"; print '<input type="hidden" name="name" value="'.$name.'">'; print '<input type="hidden" name="email" value="'.$email.'">'; print '<input type="hidden" name="message" value="'.$message.'">'; print '<input type="button" onClick="history.back()" value="戻る">'."\n"; print '<input type="submit" value="送信">'; print '</form>'; ?> </body> </html>
thanks.php
<!DOCTYPE HTML> <html lang="ja"> <head> <meta charset="utf-8"> <title>お問い合わせありがとうございました</title> <!--[if lt IE 9]> <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <!--[if lt IE 9]> <script src="http://cdnjs.cloudflare.com/ajax/libs/modernizr/2.5.3/modernizr.min.js"></script> <![endif]--> <style> article,aside,dialog,figure,footer,header,hgroup,menu,nav,section{display:block} </style> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <h1>ありがとうございました</h1> <p>お問い合わせ内容の確認メールを送信しました。</p> <?php $dsn = 'mysql:データベース名;host=ホスト名'; $user = 'ユーザーID'; $password = 'パスワード'; $dbh = new PDO($dsn, $user, $password); $dbh -> query('SET NAMES UTF8'); $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; $name = htmlspecialchars($_POST['name'],ENT_QUOTES); $email = htmlspecialchars($_POST['email'],ENT_QUOTES); $message = htmlspecialchars($_POST['message'],ENT_QUOTES); print $name.'様<br>'."\n"; print 'お問い合わせありがとうございました<br>'."\n"; print 'お問い合わせ内容『'.$message.'』を<br>'."\n"; print $email.'にメールで送りましたのでご確認ください'."\n"; $mail_sub = 'お問い合わせを受け付けました'; $mail_body = $name.'様ご協力ありがとうございました。'; $mail_head = 'From:メールアドレス@gmail.com'; mb_language('japanese'); mb_internal_encoding('UTF-8'); mb_send_mail($email,$mail_sub,$mail_body,$mail_head); $sql = 'INSERT INTO テーブル名(name,email,message) VALUES("'.$name.'","'.$email.'","'.$message.'")'; $stmt = $dbh -> prepare($sql); $stmt -> execute(); $dbh = null; ?> </body> </html>
完成したページはこちら
http://portfolio.tank.jp/contact02/