前提
PHPで以下の様な形式でお問合せフォームを作っています。
contact(お問合せ入力画面) -> confirm(確認) -> complete (完了->トップに戻る)
contact (お問合せ入力画面)-> edit(編集) -> update(編集完了)
contact(お問合せ入力画面) -> delete(削除)
今現在編集画面にてバリデーションをかけようとしているがかからいない状況。
(お問合せ入力画面ではバリデーションが問題なくかけることができる。編集画面ではバリデーションがかからない状況です。)
実現したいこと
contact(お問合せ入力画面)と同じように編集画面でもバリデーションをかけたいです。
発生している問題・エラーメッセージ
編集画面にてバリデーションがかからない状況
該当のソースコード
contact.php
<?php require_once('../Controllers/ContactController.php'); require_once('../spchars.php'); session_start(); $contacts = new ContactController(); $result = $contacts->index(); //配列の初期化 $_POST = [ 'name' => '', 'kana' => '', 'tel' => '', 'email' => '', 'body' => '', ]; $error = []; // フォームの内容をチェック if ($_SERVER['REQUEST_METHOD'] === 'POST') { $_POST['name'] = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING); if ($_POST['name'] === '') { $error['name'] = 'blank'; } elseif (mb_strlen($_POST['name']) > 10) { $error['name'] = 'length'; } 【省略】 if (empty($error)) { $_SESSION["name"] = $_POST["name"]; $_SESSION["kana"] = $_POST["kana"]; $_SESSION["tel"] = $_POST["tel"]; $_SESSION["email"] = $_POST["email"]; $_SESSION["body"] = $_POST["body"]; header('Location: ./confirm.php'); exit(); } } ?> 【省略】 <h1>入力画面</h1> </div> <form action="contact.php" method="post"> <table align="center"> <tr> <th>氏名:</th> <td> <input type="text" name="name" placeholder="山田太郎" value="<?php echo h($_POST['name']); ?>" > </td> <?php if(isset($error['name']) && $error['name'] === 'blank'): ?> <p class="error">* 氏名を入力してください</p> <?php endif; ?> <?php if(isset($error['name']) && $error['name'] === 'length'): ?> <p class="error">* 10文字以内で入力してください</p> <?php endif; ?> </tr> <tr> 【省略】 <th><td><input type="submit" name="header" value="送信"></td></th> </tr> </table> <table align="center" style="margin-top: 20px;"> <tr> <th>氏名</th> <th>カタカナ</th> 【省略】 </tr> <?php foreach ($result as $s): ?> <tr> <td><?php echo $s['name'] ?></td> <td><?php echo $s['kana'] ?></td> <td><?php echo $s['tel'] ?></td> <td><?php echo $s['email'] ?></td> <td><?php echo $s['body'] ?></td> <td><a href="/edit.php?id=<?php echo $s['id'];?>">編集</a></td> <td><a href="delete.php? id=<?php echo $s['id'];?>" onclick="return confirm('本当に削除しますか?')">削除</td> </tr> <?php endforeach; ?> </table> </body> </html>
edit.php
<?php require_once('../Controllers/ContactController.php'); require_once('../spchars.php'); session_start(); try { $dsn = 'mysql:dbname=contact;host=localhost;charset=utf8'; $user = 'root'; $password = 'root'; $dbh = new PDO($dsn, $user, $password); $sth = $dbh->prepare('SELECT * FROM contacts WHERE id = :id'); $sth->execute(array(':id' => $_GET["id"])); $result = 0; $result = $sth->fetch(PDO::FETCH_ASSOC); } catch (Exception $e) { echo 'エラーが発生しました。:' . $e->getMessage(); } $_POST = [ 'name' => '', 'kana' => '', 'tel' => '', 'email' => '', 'body' => '', ]; $error = []; // フォームの内容をチェック if ($_SERVER['REQUEST_METHOD'] === 'POST') { $_POST['name'] = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING); if ($_POST['name'] === '') { $error['name'] = 'blank'; } elseif (mb_strlen($_POST['name']) > 10) { $error['name'] = 'length'; } $_POST['kana'] = filter_input(INPUT_POST, 'kana', FILTER_SANITIZE_STRING); if ($_POST['kana'] === '') { $error['kana'] = 'blank'; } elseif (mb_strlen($_POST['kana']) > 10) { $error['kana'] = 'length'; } 【省略】 if (empty($error)) { $_SESSION["name"] = $_POST["name"]; $_SESSION["kana"] = $_POST["kana"]; $_SESSION["tel"] = $_POST["tel"]; $_SESSION["email"] = $_POST["email"]; $_SESSION["body"] = $_POST["body"]; header('Location: ./update.php'); exit(); } } ?> 【省略】 <h1>編集画面</h1> </div> <form action="update.php" method="post"> <input type="hidden" name="id" value="<?php echo h($_GET["id"]); ?>"> <table align="center"> <tr> <th>氏名:</th> <td> <input type="text" name="name" placeholder="山田太郎" value="<?php echo h($result['name']); ?>" > </td> <?php if(isset($error['name']) && $error['name'] === 'blank'): ?> <p class="error">* 入力してください</p> <?php endif; ?> <?php if(isset($error['name']) && $error['name'] === 'length'): ?> <p class="error">* 10文字以内で入力してください</p> <?php endif; ?> </tr> <tr> 【省略】 <th><td><input type="submit" name="header" value="編集する"></td></th> </tr> </table> </form> </body> </html>
Contact.php(Model)
<?php require_once('../Models/Db.php'); class Contact extends Db { protected $dbh; public function __construct($dbh = null) { parent::__construct($dbh); } public function findAll():Array{ 【省略】 } public function create() { 【省略】 } public function update() { $_SESSION['id'] = htmlspecialchars($_POST['id'], ENT_QUOTES, "UTF-8"); $_SESSION['name'] = htmlspecialchars($_POST['name'], ENT_QUOTES, "UTF-8"); $_SESSION['kana'] = htmlspecialchars($_POST['kana'], ENT_QUOTES, "UTF-8"); $_SESSION['tel'] = htmlspecialchars($_POST['tel'], ENT_QUOTES, "UTF-8"); $_SESSION['email'] = htmlspecialchars($_POST['email'], ENT_QUOTES, "UTF-8"); $_SESSION['body'] = htmlspecialchars($_POST['body'], ENT_QUOTES, "UTF-8"); try { $this -> dbh -> beginTransaction(); $sth = $this -> dbh -> prepare('UPDATE contacts SET name = :name, kana = :kana, tel = :tel, email = :email, body = :body WHERE id = :id'); $sth->bindValue(':id', $_SESSION['id'], PDO::PARAM_INT); $sth->bindValue(':name', $_SESSION['name'], PDO::PARAM_STR); $sth->bindValue(':kana', $_SESSION['kana'], PDO::PARAM_STR); $sth->bindValue(':tel', $_SESSION['tel'], PDO::PARAM_STR); $sth->bindValue(':email', $_SESSION['email'], PDO::PARAM_STR); $sth->bindValue(':body', $_SESSION['body'], PDO::PARAM_STR); $sth->execute(); $this -> dbh -> commit(); } catch (PDOException $e) { $this -> dbh -> rollBack(); echo '接続失敗'.$e->getMessage(); exit(); } } public function edit() { $id = htmlspecialchars($_GET['id'], ENT_QUOTES, 'UTF-8'); try { $this->dbh->beginTransaction(); $sql = 'SELECT * FROM contacts WHERE id = :id'; $sth = $this->dbh->prepare($sql); $sth->execute(array(':id' => $_GET["id"])); $result = $sth->fetch(PDO::FETCH_ASSOC); $this->dbh->commit(); } catch (Exception $e) { $this->dbh->rollback(); echo 'エラーが発生しました。:' . $e->getMessage(); exit(); } return $result; } public function delete() { 【省略】 }
ContactController.php
<?php require_once('../Models/Contact.php'); class ContactController { private $request; private $Contact; public function __construct() { $this->request['get'] = $_GET; $this->request['post'] = $_POST; $this-> Contact = new Contact(); $dbh = $this->Contact->get_db_handler(); } public function index() { $Contacts = $this->Contact->findAll(); return $Contacts; } public function create() { $this->Contact->create(); } public function edit() { $ContactsEdit = $this->Contact->edit(); return $ContactsEdit; } public function update() { $this->Contact->update(); } public function delete() { $this->Contact->delete(); } }
試したこと
お問い合わせと同じようにバリデーションの記述を編集画面にも記述
回答2件
あなたの回答
tips
プレビュー