前提・実現したいこと
phpの練習でカレンダー予約システムを作っています。
予約内容を入力し予約ボタンを押すと一部の内容(予約種類、時間、名前)がカレンダーの日付のtd欄に書き込まれるようにしたいです。また、ページ遷移しても内容を保持したままにし、あとで編集・削除したりできるようにしたいと考えてます。
発生している問題と疑問
エラーは出ないがheder('Locationで送ったはずの値が送られていない。 また、仮に値が入った場合にtd欄にデータが保持したままにできるかという疑問。
該当のソースコード
indexphp
1<?php 2 3require_once('Calendar.php'); 4require_once('functions.php'); 5require_once('Calendar.php'); 6 7$cal = new \MyApp\Calendar(); 8 9 ?> 10 11<!DOCTYPE html> 12<html lang="ja"> 13<head> 14 <meta charset="utf-8"> 15 <title>schedule calendar</title> 16 <link rel="stylesheet" href="styles.css"> 17 <script type="text/javascript"></script> 18</head> 19<body> 20<h2>schedule Calendar</h2> 21<!-- <a href="weekly.php" id="week">週</a> 22<a href="index.php" id="month">月</a> --> 23<table border=1> 24 <thead> 25 <tr> 26 <th><a href="/?t=<?php echo h($cal->prev); ?>">«</a></th> 27 <th colspan="5"><?php echo h($cal->yearMonth); ?></th> 28 <th><a href="/?t=<?php echo h($cal->next); ?>">»</a></th> 29 </tr> 30 </thead> 31 <tbody> 32 <tr> 33 <td id="red" class="day">Sun</td> 34 <td class="day">Mon</td> 35 <td class="day">Tue</td> 36 <td class="day">Wed</td> 37 <td class="day">Thu</td> 38 <td class="day">Fri</td> 39 <td id="blue" class="day">Sat</td> 40 </tr> 41 <form action="" method="POST"> 42 <?php echo $cal->show(); ?> 43 </form> 44</tbody> 45 <tfoot> 46 <tr> 47 <th colspan="7"><a href="/">Today</a></th> 48 </tr> 49 </tfoot> 50</table> 51<script> 52 'use strict'; 53 const selectday = document.getElementById("selectday"); 54</script> 55</body> 56</html> 57
Calendarphp
1<?php 2 3namespace MyApp; 4 5class Calendar { 6 public $prev; 7 public $next; 8 public $yearMonth; 9 private $_thisMonth; 10 public $html; 11 12 public function __construct() { 13 try { 14 if (!isset($_GET['t']) || !preg_match('/\A\d{4}-\d{2}\z/', $_GET['t'])) { 15 throw new \Exception(); 16 } 17 $this->_thisMonth = new \DateTime($_GET['t']); 18 } catch (\Exception $e) { 19 $this->_thisMonth = new \DateTime('first day of this month'); 20 } 21 $this->prev = $this->_createPrevLink(); 22 $this->next = $this->_createNextLink(); 23 $this->yearMonth = $this->_thisMonth->format('F Y'); 24 } 25 26 private function _createPrevLink() { 27 $dt = clone $this->_thisMonth; 28 return $dt->modify('-1 month')->format('Y-m'); 29 } 30 31 private function _createNextLink() { 32 $dt = clone $this->_thisMonth; 33 return $dt->modify('+1 month')->format('Y-m'); 34 } 35 36 public function show() { 37 $tail = $this->_getTail(); 38 $body = $this->_getBody(); 39 $head = $this->_getHead(); 40 $html = '<tr>' . $tail . $body . $head . '</tr>'; 41 echo $html; 42 } 43 44 private function _getTail() { 45 $tail = ''; 46 $lastDayOfPrevMonth = new \DateTime('last day of ' . $this->yearMonth . ' -1 month'); 47 while ($lastDayOfPrevMonth->format('w') < 6) { 48 $tail = sprintf('<td class="gray"><a href="reservation.php?date=%s" id="day">%d</a></td>', 49 $lastDayOfPrevMonth->format('Y-m-d'), $lastDayOfPrevMonth->format('d')) . $tail; 50 $lastDayOfPrevMonth->sub(new \DateInterval('P1D')); 51 } 52 return $tail; 53 } 54 55 private function _getBody() { 56 $body = ''; 57 $period = new \DatePeriod( 58 new \DateTime('first day of ' . $this->yearMonth), 59 new \DateInterval('P1D'), 60 new \DateTime('first day of ' . $this->yearMonth . ' +1 month') 61 ); 62 $today = new \DateTime('today'); 63 foreach ($period as $day) { 64 if ($day->format('w') === '0') { $body .= '</tr><tr>'; } 65 $todayClass = ($day->format('Y-m-d') === $today->format('Y-m-d')) ? 'today' : ''; 66 $body .= sprintf('<td class="youbi_%d %s"> 67<a href="reservation.php?date=%s" id="day">%d</a><div> 68<a href="reservation.php"><?php echo $_GET['.data.']; ?></a></div></td>', 69 $day->format('w'), 70 $todayClass, $day->format('Y-m-d'), $day->format('d')); 71 } 72 return $body; 73 } 74 75 private function _getHead() { 76 $head = ''; 77 $firstDayOfNextMonth = new \DateTime('first day of ' . $this->yearMonth . ' +1 month'); 78 while ($firstDayOfNextMonth->format('w') > 0) { 79 $head .= sprintf('<td class="gray"><a href="reservation.php?date=%s" id="day">%d</a></td>', 80$firstDayOfNextMonth->format('Y-m-d'), $firstDayOfNextMonth->format('d')); 81 $firstDayOfNextMonth->add(new \DateInterval('P1D')); 82 } 83 return $head; 84 } 85 86} 87
reservationphp
1<?php 2 3require_once('config.php'); 4require_once('functions.php'); 5require_once('Calendar.php'); 6 7session_start(); 8 9// $dbh = connectDb(); 10 11$error = array(); 12$data= array( 13 $content, 14 $time, 15 $name 16); 17 18 if ($_SERVER["REQUEST_METHOD"] !== "POST") { 19 setToken(); 20 } else { 21 checkToken(); 22 23 if (isset($_POST['submit'])) { 24 25 if (empty($_POST['content'])) { 26 $error['content'] = "選択して下さい"; 27 } else { 28 $content = $_POST['content']; 29 } 30 31 if (empty($_POST['date'])) { 32 $error['date'] = "入力して下さい"; 33 } else { 34 $date = $_POST['date']; 35 } 36 37 if (empty($_POST['time'])) { 38 $error['time'] = "入力して下さい"; 39 } else { 40 $time = $_POST['time']; 41 } 42 43 if (empty($_POST['name'])) { 44 $error['name'] = "名前を入力して下さい"; 45 } else { 46 $name = $_POST['name']; 47 48 } 49 50 if (empty($_POST['phone'])) { 51 $error['phone'] = "電話番号を入力して下さい"; 52 } else { 53 $phone = $_POST['phone']; 54 } 55 56 if (empty($_POST['email'])) { 57 $error['email'] = "メールアドレスを入力して下さい"; 58 } else { 59 $email = $_POST['email']; 60 } 61 62 if (!empty($_POST['question'])) { 63 $question = $_POST['question']; 64 } 65 66 if (empty($error)) { 67 68 $dbh = connectDb(); 69 70 $sql = "INSERT INTO reservations 71 (content, date, time, name, phone, email, question, created) 72 VALUES 73 (:content, :date, :time, :name, :phone, :email, :question, now())"; 74 $stmt = $dbh->prepare($sql); 75 $params = array( 76 ':content' => $content, 77 ':date' => $date, 78 ':time' => $time, 79 ':name' => $name, 80 ':phone' => $phone, 81 ':email' => $email, 82 ':question' => $question 83 ); 84 $stmt->execute($params); 85 echo "予約が完了しました"; 86 $dbh = null; 87 } else { 88 echo "予約を送信できませんでした"; 89 } 90 } 91 header('Location: index.php?data=array()'); 92 exit; 93} 94 95$start_time = 8; 96$end_time = 20; 97 98?> 99<!DOCTYPE html> 100<html lang="ja"> 101<head> 102 <meta charset="utf-8"> 103 <title>schedule calendar</title> 104 <link rel="stylesheet" href="styles.css"> 105</head> 106<body> 107<h2>予約内容</h2> 108<form action="" method="POST"> 109 <table border="3" frame="box"> 110 <tr> 111 <th> 112 <label>予約の種類</label> 113 </th> 114 <td> 115 <div> 116 <select name="content" value="<?php echo h($content); ?>"> 117 <option disabled selected>選択して下さい</option> 118 <option>見学</option> 119 <option>打ち合わせ</option> 120 <option>その他</option> 121 </select> 122 </div> 123 </td><br> 124 </tr> 125 <tr> 126 <th> 127 <div> 128 <label>日時</label> 129 </th> 130 <td> 131 <input type="date" name="date" value="<?php echo $_GET['date']; ?>"> 132 <br> 133 134 <?php echo "<select name=\"time\" value=”<?php echo h($time); ?>”>"; 135 echo "<option disabeled selected>", 時間を選択して下さい, "</option>"; 136 for ($i = $start_time * 2; $i <= $end_time * 2; $i++) { 137 echo '<option>'.date("H:i", strtotime("00:00 +". $i * 30 ." minute")), '</option>'; 138 } 139 echo "</select>"; ?> 140 </td> 141 </div> 142 <br> 143 </td> 144 </tr> 145 <tr> 146 <td colspan="2"> 147 <p>*回答必須</p> 148 <div> 149 <label><span>*</span>名前</label> 150 <input type="text" name="name" value="<?php echo h($name); ?>"> 151 <br> 152 <?php if (!empty($error['name'])) { echo h($error['name']); } ?> 153 </div> 154 <div> 155 <label><span>*</span>電話番号</label> 156 <input type="text" pattern="[0-9]{3}-[0-9]{4}-[0-9]{4}" name="phone" value="<?php echo h($phone); ?>"> 157 <?php if (!empty($error['phone'])) { echo h($error['phone']); } ?> 158 </div> 159 <div> 160 <label><span>*</span>メールアドレス</label> 161 <input type="email" name="email" value="<?php echo h($email); ?>"> 162 <?php if (!empty($error['email'])) { echo h($error['email']); } ?> 163 <div> 164 </td> 165 </tr> 166 <tr> 167 <td colspan="2"> 168 <label>ご質問・ご要望は下記にご入力ください</label><br> 169 <textarea name="question" value="<?php echo h($question); ?>" rows="5" cols="50"></textarea><br> 170 </td> 171 </tr> 172 <tr> 173 <td colspan="2"> 174 <input type="submit" name="submit" class="btn" value="予約する"> 175 <input type="hidden" name="token" value="<?php echo h($_SESSION['token']); ?>"> 176 <input type="button" class="cancel_btn" value="戻る" onclick="history.back(-1)"> 177 </td> 178 </tr> 179 </table> 180</form> 181<script src="Calendar.php"> 182 'use strict'; 183 console.log(document.getElementById("selectday")); 184</script> 185</body> 186</html> 187
試したこと
reservation.php内のheader('Location〜のURLに$data変数に入れた値をvalueパラメータにしてCalendar.phpでGETで受け取る。
補足情報(FW/ツールのバージョンなど)
PHP 7.1.33