PHP表单验证未显示错误

时间:2017-01-09 10:24:01

标签: php html forms

HTML表单

<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <div class="form-group">
        <label style="padding-top: 10px;" class="col-form-label">Name</label>
        <input type="text" class="form-control" placeholder="Type your name" name="name" id="name">
        <div class="alert alert-danger" role="alert">
            <span><?php echo $nameErr ?></span>
        </div>
    </div>
    <div class="form-group">
        <label class="col-form-label">Email</label>
        <input type="email" class="form-control" placeholder="Type your email here" name="email" id="email">
        <div class="alert alert-danger" role="alert">
            <span><?php echo $mailErr ?></span>
        </div>
    </div>
    <div class="form-group">
        <label class="col-form-label">Phone Number</label>
        <input type="tel" class="form-control" placeholder="Type your phone number here" name="phone" id="phone">
    </div>
    <div class="form-group">
        <label class="col-form-label">Password</label>
        <input type="password" class="form-control" placeholder="Type a password here" name="pass" id="pass">
        <div class="alert alert-danger" role="alert">
            <span><?php echo $passErr ?></span>
        </div>
    </div>
    <div class="form-group">
        <label class="col-form-label">Confirm Password</label>
        <input type="password" class="form-control" placeholder="Retype the password here" name="cpass" id="cpass">
        <div class="alert alert-danger" role="alert">
            <span aria-hidden="true"><?php echo $cpassErr ?></span>
        </div>
    </div>
    <div class="form-group row offset-sm-9" style="padding-left: 10px;">
        <button type="submit" class="btn btn-outline-primary" name="btn" value="signup">Register</button>
    </div>
</form>

PHP代码

$nameErr = $mailErr = $passErr = $cpassErr = "";

if($_SERVER["REQUEST_METHOD"] == "POST"){
    if(empty($_POST['name'])){
        $nameErr = "Name is required !";
    }elseif (empty($_POST['email'])) {
         $mailErr = "Email is required !";
    }elseif (empty($_POST['pass']) || ($_POST['pass'] < 6)) {
         $passErr = "Password is required !";
         $passErr = "At least 6 characters required !";
    }elseif (empty($_POST['cpass']) || ($_POST['pass']) != ($_POST['cpass'])) {
         $cpassErr = "Confirm password is required !";
         $cpassErr = "Confirmation password do not match !";
    }
}

我正在尝试用php验证表单。错误将显示在带有bootstap警报类的div中。甚至在点击提交按钮之前,问题就出现了那个div中未定义变量的错误。当用户点击提交按钮时,没有任何事情发生

3 个答案:

答案 0 :(得分:1)

您必须使用isset()

<div class="alert alert-danger" role="alert">
    <span><?php if(isset($nameErr) { echo $nameErr; } ?></span>
</div>

你可以删除一些PHP代码。     

//$nameErr = $mailErr = $passErr = $cpassErr = ""; << Remove it

if($_SERVER["REQUEST_METHOD"] == "POST"){
    if(empty($_POST['name'])){
        $nameErr = "Name is required !";
    }elseif (empty($_POST['email'])) {
         $mailErr = "Email is required !";
    }elseif (empty($_POST['pass']) || ($_POST['pass'] < 6)) {
         $passErr = "Password is required !";
         $passErr = "At least 6 characters required !";
    }elseif (empty($_POST['cpass']) || ($_POST['pass']) != ($_POST['cpass'])) {
         $cpassErr = "Confirm password is required !";
         $cpassErr = "Confirmation password do not match !";
    }
}

答案 1 :(得分:0)

错误是因为,第一次错误变量不可用,并且您正在尝试访问它们。所以,试试这个:

<?php echo (isset($cpassErr) ? $cpassErr : ''); ?>

isset()用于检查变量是否可用。

答案 2 :(得分:0)

您可以像这样包装验证错误

<div class="form-group">
    <label class="col-form-label">Confirm Password</label>
    <input type="password" class="form-control" placeholder="Retype the password here" name="cpass" id="cpass">
    <?php if(isset($cpassErr)): ?>
       <div class="alert alert-danger" role="alert">
          <span aria-hidden="true"><?php echo $cpassErr ?></span>
       </div>
    <?php endif; ?>
</div>

或者执行类似于上一个答案的操作,但如果您设置了背景,则可以使用此功能。你也可以做类似的事情

class='<?= isset($cpassErr) ? 'alert' : ''; ?>'

如果你不想把它们包装在ifs等中。