douyujun0152 2016-03-18 07:44
浏览 28

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

I am currently learning to validate forms in PHP and parts of my code aren't producing the desired output. I'd want the code to print out Username cant be blank when the user submits the form without a username and Password cant be blank when the password field is left blank while submitting. i have marked out, with the help of comments, the lines of code that were meant to achieve this goal (refer: //DOESNT WORK). Currently, the code is successfully able to display to the user Username/Password dont match. I am running the php scripts on XAMPP. My code:

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;
}

?>

  • 写回答

1条回答 默认 最新

  • douzhenao6515 2016-03-18 07:51
    关注

    Just add it:

    change

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

    for:

    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");
    }
    

    You can change die() will stop php execution, you can just print and manage that error otherwise.

    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)