2017-02-26 1 views
2

私は1つのフォームを持っています。複数の検証エラーが発生した場合は、検証エラーを表示し、すべてのエラーを表示するか、フィールドに応じて表示する必要があります。PHPで必要なフィールドに応じてすべての検証エラーを表示するには?

たとえば、私は3つのフィールドName, EmailMobileを持っています。すべての情報が正しくない場合は、すべての検証エラーを1つずつ表示するか、いずれかのフィールドが正しくない場合は、1つの検証エラーを表示します。 私は欲しくないjQuery検証の助けを借りてエラーを表示することができます。 process.phpで

/*alert notification for script*/ 
 
$(".notification_reminder").fadeIn() 
 
.css({top:-100,position:'absolute'}) 
 
.animate({top:20}, 800, function() { 
 
    //callback 
 
});
.form-section 
 
\t \t { 
 
padding: 30px;} 
 

 
.form-section input 
 
{ 
 
margin: 10px; 
 
} 
 
    
 
.notification_section 
 
{ 
 
position: relative; 
 
z-index: 8; 
 
} 
 
.notification_reminder 
 
{ 
 
font-size:15px; 
 
width:350px; 
 
padding: 15px; 
 
background-color:#1D9365; 
 
margin: 0 auto; 
 
} 
 
.notification_reminder p 
 
{ 
 
color:#FF0; 
 
padding:3px; 
 
box-sizing: border-box; 
 
display: initial; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
 

 

 
<?php if(!empty($_GET['chk_name'])): 
 
    \t echo" 
 
\t \t <div class='notification_section'> 
 
\t \t <div class='notification_reminder'> 
 
\t \t <p> Name required Min 3 and Max 20</p> 
 
\t \t </div> 
 
\t \t </div>"; 
 
endif;?> 
 

 
<?php if(!empty($_GET['chk_email'])): 
 
    echo" 
 
\t \t <div class='notification_section'> 
 
\t \t <div class='notification_reminder'> 
 
\t \t <p> Enter valid Email address</p> 
 
\t \t <div class='cross_sign'><i class='fa fa-times' aria-hidden='true'></i></div> 
 
\t \t </div> 
 
\t \t </div>"; 
 
endif;?> 
 

 

 
<?php if(!empty($_GET['chk_mobile'])): 
 
    echo" 
 
\t \t <div class='notification_section'> 
 
\t \t <div class='notification_reminder'> 
 
\t \t <p> 10 numbers only</p> 
 
\t \t <div class='cross_sign'><i class='fa fa-times' aria-hidden='true'></i></div> 
 
\t \t </div> 
 
\t \t </div>"; 
 
endif;?> 
 

 
<div class="form-section"> 
 
<form action="process.php" method="post"> 
 
\t <input type="text" name="name" placeholder="Name"><br /> 
 
\t <input type="email" name="email" placeholder="email"><br /> 
 
\t <input type="text" name="mobile" placeholder="mobile"><br /> 
 

 
\t <input type="submit" name="submit" > 
 
</form> 
 

 
</div>

Process.php

if(isset($_POST['submit'])){ 

$name=$_POST['name']; 
$email=$_POST['email']; 
$mobile=$_POST['mobile']; 

if ($name <3 || $name >20) { 
header('Location: test2.php?chk_name=1'); 
} 

if(!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
    header('Location: test2.php?chk_email=1'); 
} 

if(strlen($mobile)!=10 || !is_numeric($mobile)) { 
    header('Location: test2.php?chk_mobile=1'); 
} 

私は、これは、単一の検証エラーを呼ぶ理由であるheaderを使用。他の方法はありますか?これで私を助けてくれますか?

+0

正確な問題は何ですか? –

答えて

2

あなたが探しているようなことをするために、私はそれほど難しくない実装を提案することができます。しかし、あなたはフォームと治療の表示を同じページで行う必要があります。

概念を説明してください: 1.見つけたすべてのエラーを含む空のタブを作成します。 2.タブにエラーがあるかどうか確認します。そうでない場合、治療を行うことができます(データベースリクエストなど) 3.エラーがあった場合は、それを表示します。

のは、それをやってみましょう:)

<?php 

//We take the post's content or put a default value (php 7 way) 
$name = $_POST['name']??''; 
$email = $_POST['email']??''; 
$mobile = $_POST['mobile']??''; 

//We create a empty errors tab 
$errors = []; 

//We will treat error only if the form was post 
if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
    if (empty($name)) { 
     $errors['name'][] = 'Please fill name input.'; 
    } else { 
     if (strlen($name) < 2) { 
      $errors['name'][] = 'Your name must to contain minimum 2 caracteres.'; 
     } 
     if (strlen($name) > 255) { 
      $errors['name'][] = 'Your name must to contain maximum 255 caracteres.'; 
     } 
     //If you need more treatments you can :) 
    } 

    if (empty($email)) { 
     $errors['email'][] = 'Please fill name input.'; 
    } else { 
     if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
      $errors['email'][] = 'Your email must to be a valid one.'; 
     } 
     //If you need more treatments you can :) 
    } 

    if (empty($mobile)) { 
     $errors['mobile'][] = 'Please fill name input.'; 
    } else { 
     //If you need more treatments you can :) 
    } 

    // Now we'll can do what we need to do, if the form is valid 
    if (empty($errors)) { 
     //All your treatments. 

     //We can redirect to the next page, if you need to send some message to it, you can save on $_SESSION (for exemple a success message) 
     header('LOCATION: nextpage.php'); 
    } 
} 


//It's better you put what's next in a new file for be clear (look into MVC Design Pattern) 
?> 

<div class="form-section"> 
    <form method="post"> <!-- No action for redirect to the same page --> 

     <input type="text" name="name" placeholder="Name"><br/> 
     <?php //This can be put in a function like : displayErrors('field') 
     if (!empty($errors['name'])) { 
      ?> 
      <ul> 
       <?php 
       foreach ($errors['name'] as $error) { 
        ?> 
        <li><?= $error ?></li> 
        <?php 
       } 
       ?> 
      </ul> 
      <?php 
     } 
     ?> 

     <input type="email" name="email" placeholder="email"><br/> 
     <?php //This can be put in a function like : displayErrors('field') 
     if (!empty($errors['email'])) { 
      ?> 
      <ul> 
       <?php 
       foreach ($errors['email'] as $error) { 
        ?> 
        <li><?= $error ?></li> 
        <?php 
       } 
       ?> 
      </ul> 
      <?php 
     } 
     ?> 

     <input type="text" name="mobile" placeholder="mobile"><br/> 
     <?php //This can be put in a function like : displayErrors('field') 
     if (!empty($errors['mobile'])) { 
      ?> 
      <ul> 
       <?php 
       foreach ($errors['mobile'] as $error) { 
        ?> 
        <li><?= $error ?></li> 
        <?php 
       } 
       ?> 
      </ul> 
      <?php 
     } 
     ?> 

     <input type="submit" name="submit"> 
    </form> 
</div> 

をあなたは私に知らせすることを躊躇しないで、より詳細な説明が必要な場合。

よろしくお願いいたします。トップ上に表示するため

編集:

<?php 

//We take the post's content or put a default value (php 7 way) 
$name = $_POST['name']??''; 
$email = $_POST['email']??''; 
$mobile = $_POST['mobile']??''; 

//We create a empty errors tab 
$errors = []; 

//We will treat error only if the form was post 
if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
    if (empty($name)) { 
     $errors['name'][] = 'Please fill name input.'; 
    } else { 
     if (strlen($name) < 2) { 
      $errors['name'][] = 'Your name must to contain minimum 2 caracteres.'; 
     } 
     if (strlen($name) > 255) { 
      $errors['name'][] = 'Your name must to contain maximum 255 caracteres.'; 
     } 
     //If you need more treatments you can :) 
    } 

    if (empty($email)) { 
     $errors['email'][] = 'Please fill name input.'; 
    } else { 
     if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
      $errors['email'][] = 'Your email must to be a valid one.'; 
     } 
     //If you need more treatments you can :) 
    } 

    if (empty($mobile)) { 
     $errors['mobile'][] = 'Please fill name input.'; 
    } else { 
     //If you need more treatments you can :) 
    } 

    // Now we'll can do what we need to do, if the form is valid 
    if (empty($errors)) { 
     //All your treatments. 

     //We can redirect to the next page, if you need to send some message to it, you can save on $_SESSION (for exemple a success message) 
     header('LOCATION: nextpage.php'); 
    } 
} 


//It's better you put what's next in a new file for be clear (look into MVC Design Pattern) 

if (!empty($errors)) { 
    ?> 
    <div class='notification_section'> 
     <?php 
     foreach ($errors as $errorsType) { // Loop on differents inputs errors 
      foreach ($errorsType as $error) { // Loop on the errors 
       ?> 
       <div class='notification_reminder'><?= $error ?></div> 
       <?php 
      } 
     } 
     ?> 
    </div> 
    <?php 
} 
?> 

<div class="form-section"> 
    <form method="post"> <!-- No action for redirect to the same page --> 

     <input type="text" name="name" placeholder="Name"><br/> 
     <input type="email" name="email" placeholder="email"><br/> 
     <input type="text" name="mobile" placeholder="mobile"><br/> 

     <input type="submit" name="submit"> 
    </form> 
</div> 
+0

Mr.Sebastienに返信してくれてありがとうございます。エラーは正しく表示されていますが、上からアニメーションできます。テキストフィールドの下に表示したくありません。私は警告のように表示する必要があります。私のスニペットをチェックすることができます。上からエラーが表示されます。 –

+0

すべてのエラーを同じ場所(ケースの上部)に表示する場合。あなたはすべてのエラーをループすることができます、私はあなたのコードを編集して表示します。 –

関連する問題