I have a form that has this code in it, so I can echo the errors as I check the fields from a class:
<?php if( isset($_POST['send'])){
$new_user = new Register();
$new_user->check_required_fields($_POST);
$new_user->display_errors();
}
?>
and the class is:
<?php
class Register extends Database
{
public $fname;
public $lname;
public $uname;
public $email;
public $pass1;
public $pass2;
public $year;
public $month;
public $day;
public $required_array;
public $error;
public $errors = array();
public function check_required_fields($required_array)
{
if(in_array('', $required_array)) {
$errors[] = "One or more fields are missing";
//var_dump($errors);
}
else
{
$errors[] = "All fields are ok";
$this->fname = $required_array['fname'];
$this->lname = $required_array['lname'];
$this->uname = $required_array['lname'];
$this->email = $required_array['email'];
$this->pass1 = $required_array['pass1'];
$this->pass2 = $required_array['pass2'];
$this->year = $required_array['year'];
$this->month = $required_array['month'];
$this->day = $required_array['day'];
}
}
public function display_errors ($errors)
{
foreach ($errors as $error){
echo $error;
}
}
For some reason it will not display the $errors array and I am not sure why? I would be greatful for any help, thanks.
the “$errors” you use in the foreach statement is the one in the function display_errors’s parameter list, and when you invoke the function, you didn’t give any parameter, so this variable would be empty
you should use $this->errors in the foreach statement