如何将我的验证代码块放入PHP函数中

时间:2015-07-08 01:19:38

标签: php

如何将验证码放入函数中?我怎么回来打电话呢?我试着把它们放在一个代码中,然后在我的表单函数中调用它们。有什么想法吗?

这是我的代码:

function validate(){
    $errors = array();
    //empty array to collect errors

     //VALIDATION CODES (NEED TO BE INSIDE A FUNCTION)


         if(empty($_POST['email']) AND filter_var($email, FILTER_VALIDATE_EMAIL) != false)
         {
             $errors[] = "email cannot be blank";
         }

         if(empty($_POST['first_name']))
         {
             $errors[] = "First Name cannot be blank";
         } 

          if(empty($_POST['last_name']))
         {
             $errors[] = "Last Name cannot be blank";
         } 


          if(empty($_POST['password']))
         {
             $errors[] = "Password cannot be blank";
         } 

           if(empty($_POST['confirm_password']) AND $_POST['password'] == $_POST['confirm_password'])
         {
             $errors[] = "Please enter matching password";
         } 


            if(empty($_POST['confirm_password']) AND $_POST['password'] == $_POST['confirm_password'])
         {
             $errors[] = "Please enter matching password";
         } 


          if(!isset($_POST['date']) || strtotime($_POST['date']) === false)
         {
             $errors[] = "Birth Date cannot be blank";
         } 


         if(!empty($errors))
         {
             //if there are errors, assign the session variable!
             $_SESSION['errors'] = $errors;
             //redirect your user back using header('location: ')
             header('Location: registration_page.php');
         }
         else
         {

             $email = $_POST['email'];
             $first_name = $_POST['first_name'];
             $last_name = $_POST['last_name'];
             $password = $_POST['password'];
             $birth_date = $_POST['date'];

             //redirect your user to the next part of the site!

         }
}

所以当我打电话给这个时,这不会起作用:

echo validate(); 

希望你能提供帮助。谢谢!

3 个答案:

答案 0 :(得分:1)

所以你说的是:

class Validation {

    public static function emailFilter($input) {
        global $_POST;
        return empty($_POST['email']) AND filter_var($input,
             FILTER_VALIDATE_EMAIL) != false ? "email cannot be blank" : false;
    }
}

或者你想要做别的事吗?

编辑1

好的,怎么样:

function filter ($input, $type) {
    if (!$input OR !$type) {
        switch ($type) {
            case "email":
                // Check email
                if (empty($_POST['email']) AND filter_var($input, FILTER_VALIDATE_EMAIL)) {
                    return "email cannot be blank";
                }
                break;
            case "first_name":
                if(empty($_POST['first_name']))
                {
                    return "First Name cannot be blank";
                }
                break;
            // And so on.
        }
    }
}

您可以通过以下方式调用它:

filter($_POST['email'], 'email');

那么:

if (!filter($_POST['email'], 'email')) {
    // The email checks out.
} else {
    $error[] = filter($_POST['email'], 'email');
}

可以使用更优雅的解决方案,但这取决于我想要的

答案 1 :(得分:0)

假设用户在填写必填字段后点击按钮,在$ _POST ['submit']或按钮的任何名称,只需添加代码,然后通过添加html文本框旁边的错误打印或者如果你愿意,只需在你的html注册页面的文本框下面打印$ error。如果错误返回零值,那么您可以在数据库中添加所有内容,然后重定向到错误检查代码的else块中的所需页面。

答案 2 :(得分:0)

我会这样做:

function validate(){
    $errors = array();
    //empty array to collect errors

     //VALIDATION CODES (NEED TO BE INSIDE A FUNCTION)


         if(empty($_POST['email']) AND filter_var($email, FILTER_VALIDATE_EMAIL) != false)
         {
             array_push($errors, "Email cannot be blank");
         }

         if(empty($_POST['first_name']))
         {
             array_push($errors, "First Name cannot be blank");
         } 

          if(empty($_POST['last_name']))
         {
             array_push($errors, "Last Name cannot be blank");
         } 


          if(empty($_POST['password']))
         {
             array_push($errors, "Password cannot be blank");
         } 

           if(empty($_POST['confirm_password']) AND $_POST['password'] == $_POST['confirm_password'])
         {
             array_push($errors, "Please enter matching password");
         } 


            if(empty($_POST['confirm_password']) AND $_POST['password'] == $_POST['confirm_password'])
         {
             array_push($errors, "Please enter matching password");
         } 


          if(!isset($_POST['date']) || strtotime($_POST['date']) === false)
         {
             array_push($errors, "Birth Date cannot be blank");
         } 


         if(!empty($errors))
         {
             //if there are errors, assign the session variable!
             $_SESSION['errors'] = implode("|", $errors);
             //redirect your user back using header('location: ')
             return 0;
             /* 
             Can't use both return & redirect, but return is more flexible.
             */
             //header('Location: registration_page.php');
         }
         else
         {

             $email = $_POST['email'];
             $first_name = $_POST['first_name'];
             $last_name = $_POST['last_name'];
             $password = $_POST['password'];
             $birth_date = $_POST['date'];
             return array("email" => $email, "first_name" => $first_name,
                    "last_name" => $last_name, "password" => $password,
                    "birth_date" => $birth_date);
             // so now you have your results in an associative array.
             // you can use print_r(validate()); to see the results, or use
             // $r = validate();  if ($r != false) { /*go places!*/}
             //redirect your user to the next part of the site!

         }
}