index.php
<?php require_once('config.php'); require_once('functions.php'); session_start(); if ($_SERVER['REQUEST_METHOD'] != "POST"){ setToken(); } else { checkToken(); $name = $_POST['name']; $email = $_POST['email']; $memo = $_POST['memo']; $error = array(); if(!filter_var($email, FILTER_VALIDATE_EMAIL)){ $error['email'] = 'メールアドレスの形式が正しくありません'; } if($email == ''){ $error['email'] = 'メールアドレスを入力してください'; } if($memo == ''){ $error['memo'] = '内容を入力してください'; } if(empty($error)){ $dbh = connectDb(); $sql = "insert into entries (name, email, memo, created, modified) values (:name, :email, :memo, now(), now())"; $stmt = $dbh->prepare($sql); $params = array( ":name" => $name, ":email" => $email, ":memo" => $memo ); $stmt->execute($params); header('Location: '.SITE_URL.'/thanks.html'); exit; } } ?> <!DOCTYPE html> <html lang="ja"> <head> <meta charset="utf-8"> <title></title> </head> <body> <h1>お問い合わせフォーム</h1> <form method="POST" action=""> <p>お名前:<input type="text" name="name" value="<?php echo h($name); ?>"></p> <p>メールアドレス*:<input type="text" name="email" value="<?php echo h($email); ?>"> <?php if($error['email']){ echo h($error['email']); } ?></p> <p>内容*:</p> <p><textarea name="memo" cols="40" rows="5"><?php echo h($memo); ?></textarea> <?php if($error['memo']){ echo h($error['memo']); } ?></p> <p><input type="submit" value="送信"></p> <input type="hidden" name="token" value="<?php echo h($_SESSION['token']); ?>"> </form> <p><a href="<?php echo ADMIN_URL; ?>">管理者ページ</a> </body> </html>
contact
<?php require_once('../config.php'); require_once('../functions.php'); $dbh = connectDb(); $entries = array(); $sql = "select * from entries where status = 'active' order by created desc"; foreach($dbh->query($sql) as $row){ array_push($entries, $row); } // var_dump($entries); // exits; ?> <!DOCTYPE html> <html lang="ja"> <head> <meta charset="utf-8"> <title>お問い合わせ一覧</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> </head> <body> <h1>一覧データ</h1> <p><span id="num"><?php echo count($entries); ?></span>件あります。</p> <ul> <?php foreach($entries as $entry): ?> <li id="entry_<?php echo h($entry['id']); ?>"><?php echo h($entry['email']); ?> <a href="edit.php?id=<?php echo h($entry['id']); ?>">[編集]</a> <span class="deleteLink" data-id="<?php echo h($entry['id']); ?>">[削除]</span> </li> <?php endforeach; ?> </ul> <style> .deleteLink{ color: blue; cursor: pointer; } </style> <p><a href="<?php echo SITE_URL; ?>">お問い合わせフォームに戻る</a></p> <script> $(function(){ $('.deleteLink').click(function(){ if (confirm("削除してもよろしいですか?")){ var num = $('#num').text(); num--; $.post('./delete.php', { id: $(this).data('id') }, function(rs){ $('#entry_' + rs).fadeOut(800); $('#num').text(num); }); } }); }); </script> </body> </html>