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