returning several errors through one variable?

时间:2015-12-04 21:19:10

标签: php

For my registration form I've got a function that checks if all the fields are correct.

If the fields are not correct a variable $errors is passed through the session with $_SESSION['errors'] = $errors; and the user is sent back to the registration page where an error message is displayed.

What I would like to do is being able to display an error message next to each incorrect inputs, so if for example a user didn't enter both his username / password and got the captcha wrong, something like this would be displayed :

[ input for username ] please enter your username

[ input for password ] a password is required

[ input for e-mail ]

[ input for captcha ] try again

I know I could create this by setting a different variable for each error, and passing them all through the session, and then check if each of them is set, but that wouldn't look very clean.

How do I achieve this ? using $errors as an array so I can stock an error for each incorrect inputs ? (I haven't learned to use them yet but I will if that's the way to do this).

Here is my code for more clarity :

            if (!isset($username) || $username == ''){
                    $errors = "";
                    gg::writeLog('registration failed - error');
                }

            if (!ctype_alnum($username)){
                    $errors = "";
                    gg::writeLog('registration failed - error');
                }

            if (gg::getUser($username)){
                    $errors = "";
                    gg::writeLog('registration failed - error');
                }

            if (gg::getUser($email, 'email')){
                    $errors = "";
                    gg::writeLog('registration failed - error');
                }

            if (!isset($password) || $password == ''){
                    $errors = "";
                    gg::writeLog('registration failed - error');
                }

            if (!isset($passwordconf)){
                    $errors = "";
                    gg::writeLog('registration failed - error');
                }

            if (!isset($_POST['captcha'])){
                    $errors = "";
                    gg::writeLog('registration failed - error');
                }

            if ($password != $passwordconf){
                    $errors = "";
                    gg::writeLog('registration failed - error');
                }

            if (filter_var($email, FILTER_VALIDATE_EMAIL) == false){
                    $errors = "";
                    gg::writeLog('registration failed - error');
                }

            if ((int)$captcha != (int)$_SESSION['captcha_answer']){
                    $errors = "";
                    gg::writeLog('registration failed - error');
                }


if ($error != false) {
            $_SESSION['errors'] = $errors;
            header('Location: ' .ggconf::get('base_url'). '/user/register.php');

        }

1 个答案:

答案 0 :(得分:1)

创建会话数组并使用它来验证表单提交中的输入错误。

$errors = //array
if (!isset($username) || $username == ''){
        $errors['username']= "username error";
        gg::writeLog('registration failed - error');
    }

  if (gg::getUser($email, 'email')){
        $errors['email']= "email error";
        gg::writeLog('registration failed - error');
    }
    ......

现在你的表格可以这样:

<?php 
if(isset($errors['username'])){
  echo $errors['username'];
}?>
//html for username input 

$errors= array(
    "username" => "bar",
    "email" => "foo",
);
  

http://php.net/manual/en/language.types.array.php