2017-02-26 107 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

如果你想在同一个地方显示每个错误(在你的案例的顶部)。你可以循环每一个错误,我会编辑我的代码来展示你。 –

相关问题