Forum Moderators: coopster
I think what was happening there was the function was continuing the foreach() after you had sent a return, so I'm using break; to stop the loop.function EmptyOrFull() {
foreach($_POST as $key => $value) {
if($key === "username" && empty($value)) {
$ret = 1;
break;
} elseif($key === "password" && empty($value)) {
$ret = 2;
break;
} else {
$ret = 0;
}
}
return $ret;
}
$requireds = Array (
'username' => '\w+',
'password' => '[a-z\d]+',
'first_name' => '\w+',
'quantity' => '\d+', // just throwing a numeric in there
'last_name' => '\w+'
);
//
$errors = check_data($requireds);
if ($errors != '') {
echo "<p>Errors in your form:</p><ul>" . $errors . "</ul>";
// output form here
}
else {
// process the form
}
//
function check_data($requireds){
$dataErrors = ''; // or you could use null
foreach($_POST AS $key=>$value){
if (! preg_match("/^$requireds[$key]$/i",$value)) {
$dataErrors .= "<li>The " . $key . " field is blank or contains invalid input.</li>";
}
return $dataErrors;
}