PHPでフォームのエラー出力が全部出力されてしまいます。
例えば、電話番号の文字数が10文字or11文字ではなかったときは、それに関するエラーのみ出力させる。
ということを行いたいです。
現状では電話番号のエラーに関してはすべて出力されてしまいます。
どのようにしてそれぞれのエラーを出力されるようにしたらよろしいでしょうか?
registration.php(新規登録フォーム) PHPの部分のみ
php
1<?php 2session_start(); 3 4if (count($_SESSION['errorMsg']) >= 1) { 5 foreach ($_SESSION['errorMsg'] as $message) { 6 echo $message; 7 echo "<br>"; 8 } 9 echo ""; 10} 11 12 13 14 15 16 ?>
confirm.php(確認フォーム) PHPの部分のみ
php
1<?php 2session_start(); 3 4$keys = array( 5 'name', 6 'email', 7 'password', 8 'tel', 9 'prefectures', 10 'hobby', 11 'gender', 12 'contact' 13); 14 15// POST で受けたキー値と同名の変数に値を代入(可変変数) 16foreach($keys as $key) { 17 $$key = $_POST[$key]; 18} 19 20// 21// 未入力チェック 22// 23 24// エラーメッセージの配列の初期化 25$errorMsg = array(); 26 27// 名前の未入力チェック 28if (empty($name)) { 29 $errorMsg[] = "名前を入力してください。"; 30} 31 32// メールアドレスの未入力チェック 33if (empty($email)) { 34 $errorMsg[] = "メールアドレスを入力してください。"; 35} 36 37// パスワードの未入力チェック 38if (empty($password)) { 39 $errorMsg[] = "パスワードを入力してください。"; 40} 41 42// 電話番号の未入力チェック 43if (empty($tel)) { 44 $errorMsg[] = "電話番号を入力してください。"; 45} 46 47// 都道府県の未入力チェック 48if ($prefectures == "選択") { 49 $errorMsg[] = "都道府県を選択してください。"; 50} 51 52 // 53 // 文字数チェック 54 // 55 56 // パスワード文字数チェック(8文字以上か) 57 if (preg_match("/^[a-zA-Z1-9]{1,7}$/", $password)) { 58 $errorMsg[] = "パスワードは8文字以上で入力してください。"; 59 } 60 61 // 電話番号の文字数チェック(10 or 11文字) 62 if (!preg_match("/0[0-9]{9,10}/", $tel)) { 63 $errorMsg[] = "電話番号は10文字か11文字で入力してください。"; 64 } 65 66 // 電話番号の形式チェック 67 if (!preg_match("/ ^(0[1-9]{1}[1-9]{0,2}) ([0-9]{2,4}) ([0-9]{0,4})$ /", $tel)) { 68 $errorMsg[] = "電話番号の形式が違います。"; 69 } 70 71 72 if (count($errorMsg) >= 1) { 73 $_SESSION['errorMsg'] = $errorMsg; 74 foreach ($errorMsg as $error) { 75 echo $error; 76 } 77 78 // 新規登録フォームへリダイレクト処理 79 header("location: registration.php"); 80 exit(); 81} else { 82 session_destroy(); 83} 84 85 ?>
入力フォームの出力部分のソースコード(registration.php) 新規登録フォーム
php
1<!DOCTYPE html> 2<html lang="ja"> 3<head> 4 <meta charset="utf-8"> 5 <title>新規登録フォーム</title> 6 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 7 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" media="screen"> 8 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css"> 9 10</head> 11<body> 12 13 14 15<form class="form-horizontal" action="confirm.php" method="post"> 16 <fieldset> 17 18 <!-- Form Name --> 19 <legend style=background-color:#65ace4;><h1 style=text-align:center;>新規登録フォーム</h1></legend> 20 21 22 <!-- パンくずリスト--> 23 <ol class="breadcrumb"> 24 <li><a href="http:/×××.×××.××.××"><span class="glyphicon glyphicon-home" aria-hidden="true"></span>トップ</a></li> 25 <li class="active">新規登録フォーム</li> 26 </ol> 27 28 29 <div class="container"> 30 31 <!-- Text input 名前--> 32 <div class="form-group" style="margin-top:30px;"> 33 <label class="col-md-4 control-label" for="name">氏名 <span class="label label-danger">必須</span></label> 34 <div class="col-md-4"> 35 <input name="name" class="form-control input-md" id="name" type="text" placeholder="例:鈴木太郎"> 36 37 </div> 38 </div> 39 40 41 42 43 <!-- Text input メールアドレス--> 44 <div class="form-group"> 45 <label class="col-md-4 control-label" for="email">メールアドレス <span class="label label-danger">必須</span></label> 46 <div class="col-md-4"> 47 <input name="email" class="form-control input-md" id="email" type="email" placeholder="例:suzuki.taro@gmail.com"> 48 49 </div> 50 </div> 51 52 53 <!-- Text input パスワード--> 54 <div class="form-group"> 55 <label class="col-md-4 control-label" for="password">パスワード <span class="label label-danger">必須</span></label> 56 <div class="col-md-4"> 57 <input name="password" class="form-control input-md" id="password" type="password" placeholder="パスワード"> 58 59 </div> 60 </div> 61 62 <!-- Text input 電話番号 --> 63 <div class="form-group"> 64 <label class="col-md-4 control-label" for="tel">電話番号 <span class="label label-danger">必須</span></label> 65 <div class="col-md-4"> 66 <input name="tel" class="form-control input-md" id="tel" type="tel" placeholder="09011112222 ハイフンなし"> 67 68 </div> 69 </div> 70 71 <!-- Select Basic 都道府県 --> 72 <div class="form-group"> 73 <label class="col-md-4 control-label" for="prefectures">都道府県 <span class="label label-danger">必須</span></label> 74 <div class="col-md-4"> 75 <select name="prefectures" class="form-control" id="prefectures"> 76 <option value="選択">選択</option> 77 <option value="東京">東京</option> 78 <option value="名古屋">名古屋</option> 79 <option value="大阪">大阪</option> 80 <option value="その他">その他</option> 81 </select> 82 </div> 83 </div> 84 85 86 <!-- インラインチェックボックス 趣味 --> 87 <div class="form-group"> 88 <label class="control-label col-md-4" for="checkbox">趣味 <span class="label label-info">任意</span></label> 89 <div class="col-md-4"> 90 <div class="checkbox"> 91 <label class="checkbox-inline"> 92 <input name="hobby[]" type="checkbox" value="野球"> 野球 93 </label> 94 <label class="checkbox-inline"> 95 <input name="hobby[]" type="checkbox" value="サッカー"> サッカー 96 </label> 97 </div> 98 </div> 99 </div> 100 101 102 103 <!-- インラインラジオボタン 性別 --> 104 <div class="form-group"> 105 <label class="control-label col-md-4" for="checkbox">性別 <span class="label label-info">任意</span></label> 106 <div class="col-md-4"> 107 <div class="radio-inline" name="gender"> 108 <input name"gender" type="radio" value="男性" name="gender" id="man">男性 109 </div> 110 <div class="radio-inline"> 111 <input name"gender" type="radio" value="女性" name="gender" id="woman">女性 112 </div> 113 </div> 114 </div> 115 116 117 118 <!-- テキストエリア お問い合わせ --> 119 <div class="form-group"> 120 <label class="col-md-4 control-label" for="contact">お問い合わせ <span class="label label-info">任意</span></label> 121 <div class="col-md-4"> 122 <textarea name="contact" placeholder="お問い合わせ" rows="3" class="form-control" id="contact"></textarea> 123 </div> 124 </div> 125 126 127 128 <!-- Button 送信ボタン --> 129 <div class="form-group"> 130 <label class="col-md-4 control-label" for="singlebutton"></label> 131 <div class="col-sm-offset-5 col-sm-7 col-xs-offset-4 col-xs-8"> 132 <a href="http://192.168.33.15"><button type="button" name="singlebutton" class="btn btn-primary" id="singlebutton">戻る</button></a> 133 <button name="singlebutton" class="btn btn-primary" id="singlebutton">確認する</button> 134 </div> 135 </div> 136 137 </div> 138 139 </fieldset> 140 </form> 141 142 143 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 144 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> 145 </body> 146 </html> 147