gpt4 book ai didi

php忽略重定向if语句

转载 作者:行者123 更新时间:2023-11-28 02:45:52 25 4
gpt4 key购买 nike

我正在制作一个包含各种输入字段的注册表单。我有 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();
}

最佳答案

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

}
?>

关于php忽略重定向if语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41580716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com