I’m trying to implement an error checking system with PHP (too few characters, blank input, etc…).
Right now, if I leave a blank title, the error is caught but it’s not displaying the message. Here’s the first part which actually checks the input (‘ll just show for an event title)
Class check_errors{
function create_event_errors($event_title){
global $submit_error;
if($event_title == ""){
$field = "event_title";
$submit_error->setError($field,"Title cannot be blank");
}
elseif(strlen($event_title) < 5){
$field = "event_title";
$submit_error->setError($field,"Title must be longer than 5 characters");
}
elseif(strlen($event_title) > 75){
$field = "title";
$submit_error->generalError($field,"Title must be less than 40 characters");
}
}
So, on the same page, here’s the display_errors class
Class display_errors{
var $values = array(); //Holds submitted form field values
var $errors = array(); //Holds submitted form error messages
function setValue($field, $value){
$this->values[$field] = $value;
}
function setError($field, $errmsg){
$this->errors[$field] = $errmsg; //if I echo this, the error message displays
$this->num_errors = count($this->errors); //if I echo this, the # of errors displays
}
function value($field){
if(array_key_exists($field,$this->values)){
return htmlspecialchars(stripslashes($this->values[$field]));
}else{
return "";
}
}
function error($field){
if(array_key_exists($field,$this->errors)){
return $this->errors[$field];
}else{
return "";
}
}
}
Now for the issue. When I try to check if errors are detected, it doesn’t find anything
$check_errors->create_event_errors($event_title); //submit the input to be checked
echo $display_errors->num_errors; //this does not display the # of errors. It does when I echo from within the function in the class though
if($display_errors->num_errors == 0){
//submit event
}
Don’t use
var, don’t useglobaland define all properties. Also consider @harke’s pointers for better code design.Run the code