我已经采用了JPMaster77创建found here的部分错误检查系统。它在我的一个页面上完美工作,但在另一个页面上,错误完全不显示。
所以,当用户访问一个页面时,会显示一个表单。这个表单调用一个函数,该函数有一个错误数组,每次加载页面(你会看到更多)。提交表单时,会检查错误。如果找到它,它将被发送到错误处理类。数组被设置为这个错误。表单调用一个函数,然后返回存储在这个数组中的错误。
截至目前,错误是设置,我可以看到数组与错误设置。但是,当表单调用一个函数来检索这个数组时,该数组就是空的。我无法弄清楚是什么原因造成的。
此代码在edit.php上显示我的表单并提交表单。这是我在我的网页上的确切顺序。
if(isset($_POST['submitForm'])){
//send form to be checked
}
if(isset($_GET['eventID'])){
$getForm->eventDetails($eventID); //displays a form
?>
}这是正在显示的表单。注意我如何使用$ error-> error(“eventTitle”)。这会在error.php中调用我的函数错误,并且如果使用该键设置了错误,则会显示错误消息。
//yes I know the errors aren't in cells, I'll change that. It still displays text above the table when I echo something else there
Event Title: | |
Event Description: | |
Event Location: |
if(preg_match('/[0-9]/',$event['eventDate'])){
list($yyyy,$mm,$dd) = explode('-',$event['eventDate']);
$newDate = $mm."-".$dd."-".$yyyy;
}
?>
Event Date: echo $error->error("eventDate"); ?>Event Time: echo $error->error("eventTime"); ?>表单提交后,它将被发送以进行检查。这是错误检查函数的代码的一个小窍门(它很大,所以我只会展示如何捕获一个特定的错误)if(strlen($title) == 0){
$field = "eventTitle";
$msg = "Title cannot be blank";
$error->setError($field,$msg);
}如果有错误,它会发送到我的错误类中的setError函数。
var $values = array();
var $errors = array();
function setError($field, $errmsg){
$this->errors[$field] = $errmsg;
$this->num_errors = count($this->errors);
echo $this->errors[$field]; //This is just for testing. The proper error is displayed here
}
//this is the function that gets called from the form.
function error($field){
if(array_key_exists($field,$this->errors)){
return "".$this->errors[$field].""; //th
}else{
return "";
}
}奇怪的是,当我在setError中对数组执行print_r操作时,它会显示带有错误的已填充数组。但是,当我在错误函数中做同样的事情时,它是空的。就好像数组已经从重新加载或其他地方清除一样。
当我在我的setError函数中使用print_r($ this-> errors)时,数组被填充。当我在我的错误函数中做同样的事情时,数组是空的。