我有一张表格。如果出现多个验证错误,则必须显示验证错误,然后根据字段要求显示所有错误或显示。
例如 - 我有三个字段Name, Email
和Mobile
。如果所有信息都不正确,则逐个显示所有验证错误,或者任何一个字段不正确,然后显示一个验证错误。
我可以借助我不想要的jQuery验证来显示错误。
/*alert notification for script*/
$(".notification_reminder").fadeIn()
.css({top:-100,position:'absolute'})
.animate({top:20}, 800, function() {
//callback
});
.form-section
{
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'])):
echo"
<div class='notification_section'>
<div class='notification_reminder'>
<p> Name required Min 3 and Max 20</p>
</div>
</div>";
endif;?>
<?php if(!empty($_GET['chk_email'])):
echo"
<div class='notification_section'>
<div class='notification_reminder'>
<p> Enter valid Email address</p>
<div class='cross_sign'><i class='fa fa-times' aria-hidden='true'></i></div>
</div>
</div>";
endif;?>
<?php if(!empty($_GET['chk_mobile'])):
echo"
<div class='notification_section'>
<div class='notification_reminder'>
<p> 10 numbers only</p>
<div class='cross_sign'><i class='fa fa-times' aria-hidden='true'></i></div>
</div>
</div>";
endif;?>
<div class="form-section">
<form action="process.php" method="post">
<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>
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>