質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Q&A

解決済

2回答

5997閲覧

PHPで入力フォームのエラーを一気に出さないようにするにはどうしたら良いでしょうか?

dog57

総合スコア131

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

0グッド

0クリップ

投稿2017/11/27 13:23

編集2017/11/28 04:56

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>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

サンプル

php

1<?php 2 3// 開発時にこれを書かないのはNG 4ini_set('display_errors', TRUE); 5error_reporting(E_ALL); 6 7// セッションスタート 8session_start(); 9 10// XSS対策 11function h($string) 12{ 13 return htmlspecialchars($string, ENT_QUOTES, 'utf-8'); 14} 15 16// POST時 17if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') == 'POST') { 18 19 $name = filter_input(INPUT_POST, 'name'); 20 $email = filter_input(INPUT_POST, 'email'); 21 22 $errors = []; 23 if (empty($name)) { 24 $errors['name'] = '名前は入力必須です。'; 25 } 26 if (empty($email)) { 27 $errors['email'] = 'メールは入力必須です。'; 28 } 29 30 // エラーがなければ確認ページへ 31 if (count($errors) === 0) { 32 $_SESSION = filter_input_array(INPUT_POST); 33 header('location: confirm.php'); 34 } 35} 36?><!DOCTYPE HTML> 37<html lang="ja"> 38 <head> 39 <meta charset="UTF-8"> 40 <title>サンプル</title> 41 <style type="text/css"> 42 .red { 43 color: red; 44 } 45 </style> 46 </head> 47 <body> 48 <form action="" method="post"> 49 <?php if (isset($errors) && count($errors)) : ?> 50 <div> 51 <?php foreach ($errors as $error) : ?> 52 <p class="red"><?= h($error); ?></p> 53 <?php endforeach; ?> 54 </div> 55 <?php endif; ?> 56 <div> 57 <label>必須&nbsp;名前:<input type="text" name="name" value="<?= h(filter_input(INPUT_POST, 'name')); ?>" /></label> 58 </div> 59 <div> 60 <label>必須&nbsp;メール:<input type="text" name="email" value="<?= h(filter_input(INPUT_POST, 'email')); ?>" /></label> 61 </div> 62 <div> 63 <button>送信</button> 64 </div> 65 </form> 66 </body> 67</html>

php

1 2/** 3 * confirm.php 4 */ 5<?php 6ini_set('display_errors', TRUE); 7error_reporting(E_ALL); 8 9session_start(); 10?><!DOCTYPE HTML> 11<html lang="ja"> 12 <head> 13 <meta charset="UTF-8"> 14 <title></title> 15 </head> 16 <body> 17 <?php var_dump($_SESSION); ?> 18 </body> 19</html>

投稿2017/11/27 13:59

編集2017/11/27 14:00
退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

dog57

2017/11/28 00:46

ありがとうございます。こちらを参考にコーディングしてみたいと思います。
guest

0

メアドとパスワードの文字数チェック(1文字以上入力されているかどうか)を調べる処理を入れればいいだけです。

投稿2017/11/28 00:42

zico_teratail

総合スコア907

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問