PHP表单验证:我的代码有什么问题?

时间:2016-03-18 07:44:54

标签: php forms validation debugging

我目前正在学习用PHP验证表单,而我的部分代码并没有产生所需的输出。我希望代码在用户提交没有用户名的表单时打印Username cant be blank,并在提交时将密码字段留空时Password cant be blank。我已经在评论的帮助下标出了旨在实现这一目标的代码行(参见://DOESNT WORK)。目前,代码已成功显示给用户Username/Password dont match。我在XAMPP上运行php脚本。我的代码:

form_with_validation.php

<?php 
require_once("included_functions.php");
require_once("validation_functions.php");

$errors = array();
$message = "";

if(isset($_POST['submit']))
{   //form was submitted
    $username = trim($_POST["username"]);
    $password = trim($_POST["password"]);

    //Validations
    $fields_required = array("username", "password");
    foreach($fields_required as $field) //DOESNT WORK
    {
        $value = trim($_POST[$field]);
        if(!has_presence($value))
        {
            $errors[$field] = ucfirst($field) . " cant be blank.";
        }
    }

    if(empty($errors))
    {
         //try to login
        if($username == "mickey" && $password == "password")
        {   //successful login
            redirect_to("basic.html");
        }
        else
        {
            $message = "Username/Password dont match.";  
        }   
    }

    }
    else {
    $username = "";
    $message = "Please Log in.";
    }
?>
<html lang = "en">
<head>
    <title>Form</title>
</head>

<body>
    <?php echo $message; ?> <br>
    <?php echo form_errors($errors);?>

    <form action="form_with_validation.php" method = "post">
        Username: <input type="text" name="username" value="<?php echo htmlspecialchars($username)?>" /><br>
        Password: <input type="password" name="password" value=""/><br>
        <br>
        <input type="submit" name= "submit" value="Submit" />
    </form>
</body>

validation_functions.php

<?php

//presence
function has_presence($value)
{
    return isset($value) || $value !== "";
}

//string length
    //max length
function has_max_length($value, $max)
{
    return strlen($value) <= $max;
}

//inclusion in a set
function has_inclusion_in($value, $set)
{
    return in_array($value, $set);
}

function form_errors($errors=array())
{
    $output = "";
    if(!empty($errors))
    {
        $output .= "<div class=\"error\">";
        $output .= "Please fix the following errors:"; //NOT WORKING
        $output .= "<ul>";
        foreach($errors as $key => $error)
        {
            $output .= "<li>{$error}<li>";
        }
        $output = "</ul>";
        $output .= "</div>";
    }
    return $output;
}

?> 

included_functions.php     

{ 
    return "Hello {$name}!";
}
function redirect_to($new_location)
{
    header("Location: " . $new_location);
    exit;
}

&GT;

1 个答案:

答案 0 :(得分:0)

只需添加它:

更改

$username = trim($_POST["username"]);
$password = trim($_POST["password"]);

有:

if(isset($username)){
    $username = trim($_POST["username"]);
}else{
    die("Username cant be blank");
}
if(isset($password)){
    $password = trim($_POST["password"]);
}else{
    die("password cant be blank");
}

您可以更改die()将停止执行php,否则您只需打印并管理该错误。