我正在制作一个包含各种输入字段的注册表单。我有1个字段为空时的验证代码,以及当整个表格为空时的验证代码。当表单为空时,它将重定向回寄存器页面。但是如果我填写1个输入字段,他仍然运行空表单重定向而不是忽略那个并继续下一个if语句。
你们可以帮帮我吗?
以下是HTML代码:
<form class="register form" action="../app/controller/authController.php" method="POST">
<div class="form-group">
<select name="region" id="">
<option value="Americas">Americas</option>
<option value="Europe">Europe</option>
<option value="Asia">Asia</option>
</select>
</div>
<div class="form-group">
<input type="text" name="first_name" id="" placeholder="First Name">
<?php if(isset($_SESSION['FN_error'] )) : ?>
<span class="error"><?php echo $_SESSION['FN_error']; ?></span>
<?php unset($_SESSION['FN_error']); endif; ?>
</div>
<div class="form-group">
<input type="text" name="last_name" id="" placeholder="Last Name">
<?php if(isset($_SESSION['LN_error'] )) : ?>
<span class="error"><?php echo $_SESSION['LN_error']; ?></span>
<?php unset($_SESSION['LN_error']); endif; ?>
</div>
<div class="form-group">
<input type="email" name="email" id="" placeholder="E-mail Address">
<?php if(isset($_SESSION['EA_error'] )) : ?>
<span class="error"><?php echo $_SESSION['EA_error']; ?></span>
<?php unset($_SESSION['EA_error']); endif; ?>
</div>
<div class="form-group">
<input type="email" name="confirm_email" id="" placeholder="Confirm E-mail Address">
<?php if(isset($_SESSION['Confirm_email_error'])) : ?>
<span class="error"><?php echo $_SESSION['Confirm_email_error']; ?></span>
<?php unset($_SESSION['Confirm_email_error']); endif; ?>
</div>
<div class="form-group">
<input type="password" name="password" id="" placeholder="Password">
<?php if(isset($_SESSION['PS_error'] )) : ?>
<span class="error"><?php echo $_SESSION['PS_error']; ?></span>
<?php unset($_SESSION['PS_error']); endif; ?>
</div>
<div class="form-group">
<input type="password" name="confirm_password" id="" placeholder="Confirm Password">
<?php if(isset($_SESSION['Confirm_pass_error'] )) : ?>
<span class="error"><?php echo $_SESSION['Confirm_pass_error']; ?></span>
<?php unset($_SESSION['Confirm_pass_error']); endif; ?>
</div>
<div class="form-group">
<input type="text" name="age" id="" placeholder="Age">
<?php if(isset($_SESSION['AGE_error'] )) : ?>
<span class="error"><?php echo $_SESSION['AGE_error']; ?></span>
<?php unset($_SESSION['AGE_error']); endif; ?>
</div>
<div class="form-group">
<input type="submit" name="type" value="Register" class="register button">
</div>
</form>
这是PHP代码:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if ($_POST['type'] == 'Register') {
if (trim($_POST['first_name']) == "") {
$_SESSION['FN_error'] = "Field cannot be empty!";
$user->redirect('register.php');
exit();
}
if (trim($_POST['last_name']) == "") {
$_SESSION['LN_error'] = "Field cannot be empty!";
$user->redirect('register.php');
exit();
}
if (trim($_POST['email']) == "") {
$_SESSION['EA_error'] = "Field cannot be empty!";
$user->redirect('register.php');
exit();
}
if (trim($_POST['password']) == "") {
$_SESSION['PS_error'] = "Field cannot be empty!";
$user->redirect('register.php');
exit();
}
if (trim($_POST['age']) == "") {
$_SESSION['AGE_error'] = "Field cannot be empty!";
$user->redirect('register.php');
exit();
}
if (trim($_POST['confirm_email']) == "") {
$_SESSION['Confirm_email_error'] = "Field cannot be empty!";
$user->redirect('register.php');
exit();
} elseif($_POST['email'] != $_POST['confirm_email']) {
$_SESSION['Confirm_email_error'] = "E-mail Address does not match!";
$user->redirect('register.php');
exit();
}
if (trim($_POST['confirm_password']) == "") {
$_SESSION['Confirm_pass_error'] = "Field cannot be empty!";
$user->redirect('register.php');
exit();
} elseif($_POST['password'] != $_POST['confirm_password']) {
$_SESSION['Confirm_pass_error'] = "Password does not match!";
$user->redirect('register.php');
exit();
}
答案 0 :(得分:0)
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$i=0;
$names=array('first_name', 'last_name','email','password','age','confirm_email');
foreach($names as $field) {
if (empty($_POST[$field])) {
$_SESSION[$field.'error'] = "Field cannot be empty!";
$i++;
}
else
{
unset($_SESSION[$field.'error']);
}
}
if(sizeof($names)==$i)
{
$user->redirect('register.php');
}
}
?>