2017-02-26 103 views
2

我有一種形式。如果出現多個驗證錯誤,則必須顯示驗證錯誤,然後根據字段要求顯示所有錯誤或顯示。如何根據PHP中所需的字段顯示所有驗證錯誤?

例如,我有三個字段Name, EmailMobile。如果所有信息都不正確,則逐個顯示所有驗證錯誤或任何一個字段不正確,然後顯示一個驗證錯誤。 我可以在jQuery驗證的幫助下顯示錯誤,我不想要。

/*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'); 
} 

在process.php我用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

感謝您答覆Sebastien先生,錯誤顯示正確,但我們可以從頂部對它進行動畫處理。我不想在文本框的下方顯示。我必須像警示一樣顯示。你可以檢查我的代碼片段。錯誤將從頂部顯示。 –

+0

如果你想在同一個地方顯示每個錯誤(在你的案例的頂部)。你可以循環每一個錯誤,我會編輯我的代碼來展示你。 –

相關問題