Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP表单验证未显示错误_Php_Html_Forms - Fatal编程技术网

PHP表单验证未显示错误

PHP表单验证未显示错误,php,html,forms,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="for

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 echo (isset($cpassErr) ? $cpassErr : ''); ?>


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

您可以这样包装验证错误

<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' : ''; ?>'
class=''

如果不想将它们包装在ifs等中,则必须使用
isset()


class='<?= isset($cpassErr) ? 'alert' : ''; ?>'
<div class="alert alert-danger" role="alert">
    <span><?php if(isset($nameErr) { echo $nameErr; } ?></span>
</div>
//$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 !";
    }
}