dongzong5017 2016-12-30 14:38
浏览 37
已采纳

有更好的方法来验证我的表单吗? 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

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';
        }
    }
}
  • 写回答

2条回答 默认 最新

  • douping1993 2016-12-30 14:45
    关注
    <?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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真