简体   繁体   English

有没有更好的方法来验证我的表格? 的PHP

[英]Is there a better way to validate my form? PHP

I am trying to create a register page and I was wondering if there is a better way of doing this. 我正在尝试创建一个注册页面,我想知道是否有更好的方法可以做到这一点。 So what I was think was instead of typing every field or is this a good way of doing this? 因此,我认为不是要键入每个字段,还是这样做的一种好方法?

This is what I have currently. 这就是我目前所拥有的。

This is the form 这是表格

<form action="" method="POST">
    <input type="text" name="username" placeholder="Username:" />
    <input type="password" name="password" placeholder="Password:" />
    <input type="password" name="password" placeholder="Confirm password:" />
    <input type="text" name="email" placeholder="Email:" />
    <input type="text" name="first_name" placeholder="First name:" />
    <input type="text" name="last_name" placeholder="Last name:" />
    <input type="submit" name="register" value="REGISTER" />
</form>

This is what I have for PHP side 这就是我对PHP的要求

function reg_validation() {    
     if ($_POST['register']) {   
        if (empty($_POST['username'])) {
            echo 'Please enter a username';
        }
        if (empty($_POST['password'])) {
            echo 'Please enter a password';
        }
        if (empty($_POST['confirm_password'])) {
            echo 'Please confirm your password';
        }
        if (empty($_POST['email'])) {
            echo 'Please enter an email address';
        }
        if (empty($_POST['first_name'])) {
            echo 'Please enter your first name';
        }
        if (empty($_POST['last_name'])) {
            echo 'Please enter your last name';
        }
    }
}
<?php

function reg_validation() {    
    if (isset($_POST['register'])) {
        $expectedFields = array(
            'username', 'password', 'confirm_password', 'email', 'first_name', 'last_name'
        );

        foreach ($expectedFields as $value)
            if (!array_key_exists($value, $_POST) || empty($_POST[$value]))
                return false;

        return true;
    }

    return false;
}

?>

Don't know if I misunderstood your question in my last answer, so here is another suggestion. 不知道我是否在上次回答中误解了您的问题,所以这是另一个建议。 Code is not tested, so it may contain minor typos. 代码未经测试,因此可能包含少量错字。

In this suggestion we store all field names in an array an loop through the array and checks if any of the field names is not a key in the post array. 在此建议中,我们将所有字段名存储在数组中,并遍历该数组,并检查是否任何字段名都不是post数组中的键。

So this function will return true if all fields is found in post data or false if not. 因此,如果在发布数据中找到所有字段,则此函数将返回true,否则将返回false。

<?php

if (isset($_POST['register'])) {
    try {
        if (empty($_POST['username']))
            throw new Exception('missing_username');
        else if (empty($_POST['password']))
            throw new Exception('missing_password');
        else if (empty($_POST['confirm_password']))
            throw new Exception('missing_confirm_pass');
        else if (empty($_POST['email']))
            throw new Exception('missing_email');
        else if (empty($_POST['first_name']))
            throw new Exception('missing_firstname');
        else if (empty($_POST['last_name']))
            throw new Exception('missing_lastname');

        // do something if all fields are filled
    } catch (Exception $e) {
        $errorArray = array(
            'missing_username' => 'Please enter a username',
            'missing_password' => 'Please enter a password',
            'missing_confirm_pass' => 'Please confirm your password',
            'missing_email' => 'Please enter an email address',
            'missing_firstname' => 'Please enter your first name',
            'missing_lastname' => 'Please enter your last name'
        );

        if (isset($errorArray[$e->getMessage()]))
            echo $errorArray[$e->getMessage()];
    }
}

?>

I like to do it this way, don't know what everyone else thinks about it. 我喜欢这样做,不知道其他人怎么看。

By using try catch, we don't neccessarily need a function to return, since we instead can throw an exception to break from the try section. 通过使用try catch,我们不需要返回一个函数,因为我们可以抛出一个异常以从try部分中断。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM