如何在不使用$ _GET - PHP的情况下发送和接收表单验证错误

时间:2013-11-03 14:10:57

标签: php

我试图在PHP表单验证中发送和接收错误,而不必使用$ _GET方法。我认为我会做的是在我的functions.php(可重用函数和变量)文件中创建一个错误数组,如下所示:

$errors = array();

并像这样操纵错误数组:

//do work with errors array
        //set error into array
        function setError($error){
            $errors[] = $error;
        }

        //get information from array
        function getError(){
            return $errors;
        }

        //empty errors array
        function emptyErrorsArray(){
            $errors[] = null;
        }

        //print errors array
        function printErrorsArray(){
            var_dump($errors);
        }

然而,当我执行上述方法时,我得不到对表单的反馈,因为错误数组整个时间都是NULL。我尝试将$ errors数组设置为全局变量,但这对我来说也不是很好。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您应该使用if else语句进行PHP验证,如下所示:

if(condition)
{

do this

}
else 
{

$errors[] = "your error message" 

} 

after that go just under the body tag and in php blocks put:

if($errors != "")

{

foreach($errors as $error)

{

echo $error;

}

}

This is to display the errors