简体   繁体   English

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

[英]How to send and receive errors for form validation without using $_GET - PHP

I am trying to send and receive errors in php form validation without having to use the $_GET method. 我正在尝试使用php表单验证发送和接收错误,而不必使用$ _GET方法。 What I thought I would do was create an errors array in my functions.php (reusable functions and variables) file like so: 我想做的是在我的functions.php(可重复使用的函数和变量)文件中创建一个错误数组,如下所示:

$errors = array();

And manipulate that errors array like so: 像这样操作该错误数组:

//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);
        }

However when I do the above method, I get no feedback to my form as the errors array says NULL the whole time. 但是,当我执行上述方法时,我的表单没有得到任何反馈,因为错误数组始终都为NULL。 I tried setting the $errors array to a global variable but that didn't work out so well for me as well. 我尝试将$ errors数组设置为全局变量,但这对我来说也不太理想。 Any ideas? 有任何想法吗?

You should be using if else statements for PHP validation like so : 您应该使用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

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

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