php脚本无输出

时间:2018-10-14 20:06:12

标签: php html validation

我有一个usernamepassword框和一个login按钮。我的php脚本应检查框是否为空以及所传递内容的长度是否少于6个字符。它没有按照我想要的去做,我不确定为什么。我是php新手,所以我使用W3 Schools作为参考,但似乎无法弄清楚。我将在下面粘贴代码。

<?php

if($_POST){

#not empty
#atleast 6 characters long


# array holds errors
$errors = array();

# validation starts here
if(empty($_POST['uname'])){
    $errors['$uname1'] = "Your name cannot be empty";
}

if(strlen($_POST['uname']) < 6){
    $errors['uname2'] = "Must be longer than 6 characters";
}

if(empty($_POST['psw'])){
    $errors['psw1'] = "Your password cannot be empty";
}

if(strlen($_POST['psw']) < 6){
    $errors['psw2'] = "Must be longer than 6 characters";
}


if(count($errors) == 0){

    # redirect to the game page
    header('Location:success.html');
    exit();
}
}


?>


<!DOCTYPE html>

<head>
    <title>Hangman Home Page</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>

    <form action="" method="post" class="modal-content animate">



        <div class="container">
            </p>

            <label for="uname"><b>Username</b></label>
            <input type="text" placeholder="Enter Username" name="uname">
            <p>
                <?php if(isset($errors['uname1'])) echo $errors['uname1'];  ?>
            </p>
            <p>
                <?php if(isset($errors['uname2'])) echo $errors['uname2'];  ?>
            </p>


            <label for="psw"><b>Password</b></label>
            <input type="password" placeholder="Enter Password" name="psw">
            <p>
                <?php if(isset($errors['psw1'])) echo $errors['psw1'];  ?>
            </p>
            <p>
                <?php if(isset($errors['psw2'])) echo $errors['psw2'];  ?>
            </p>

            <button type="submit" value="submit">Login</button>
        </div>
    </form>

</body>

</html>

1 个答案:

答案 0 :(得分:0)

尝试以下示例。它基于称为 null合并运算符的PHP7功能。正如documentation所说:

  

如果存在并且不为NULL,则返回其第一个操作数;否则,它将返回其第二个操作数。

因此,您可以这样写:$uname = $_POST['uname'] ?? '';,这意味着如果$uname存在且不等于NULL,则$_POST['uname']必须为$_POST['uname']。否则,它等于空字符串''

回声错误为<?php echo $errors['psw'] ?? ''; ?>。如果设置了错误且不为null,则回显它们。否则回显空字符串。

也无需检查字符串是否为空。因为如果字符串为空-则保证字符串长度小于6。

<?php

$uname = $_POST['uname'] ?? '';
$psw = $_POST['psw'] ?? '';

$errors = [];

if(strlen($uname) < 6) $errors['uname'] = 'Name must be longer than 6 characters';
if(strlen($psw) < 6) $errors['psw'] = 'Password must be longer than 6 characters';

if(empty($errors)) {
    header('Location:success.html');
    exit();
}
?>

<!DOCTYPE html>

<head>
    <title>Hangman Home Page</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>

    <form action="" method="post" class="modal-content animate">



        <div class="container">
            </p>

            <label for="uname"><b>Username</b></label>
            <input type="text" placeholder="Enter Username" name="uname">
            <p><?php echo $errors['uname'] ?? '';  ?></p>


            <label for="psw"><b>Password</b></label>
            <input type="password" placeholder="Enter Password" name="psw">
            <p><?php echo $errors['psw'] ?? '';  ?></p>

            <button type="submit" value="submit">Login</button>
        </div>
    </form>

</body>

</html>