I'm validating forms through a series of functions. The only way I can seem to return error messages is through a global variable. I've been reading this is less than ideal. Below is my basic page setup.
I'm not seeing another way around this unless I create a function that also is responsible for displaying the form. I'm hoping to not have to do that. It always seems harder to edit the HTML if it's being echo'd out.
The form posts to itself and calls an external PHP file to handle the validation.
HTML Setup
html...
html...
html...
<form action="<?php $_SERVER['PHP_SELF'] ?>"></form>
// If an error has been found echo the error message above the form
<?php if (isset($error)) { echo "<h2 class=\"alert clear\">There were errors with the information you've entered.</h2>"; } ?>
External PHP Form Validation
// Determines what action to take based on type of form submitted
switch ($action) {
case 'processForm' :
processForm();
break;
default :
header('Location: index.php');
}
// First set of validation
function checkSet() {
return isset($_POST['billFirst'], $_POST['billLast'], ...);
}
// Calls the first check and if passes proceeds with the second set
function checkFields() {
global $error;
if (checkSet() != FALSE) {
if (empty($_POST['billFirst']) == TRUE || valName($_POST['billFirst'], 'string') != TRUE) {
$error['billFirst'] = 'T';
}
if (empty($_POST['billLast']) == TRUE || valName($_POST['billLast'], 'string') != TRUE) {
$error['billLast'] = 'T';
}
}
else {
$error[] = 'T';
}
return (!isset($error));
}
function processForm() {
if (checkFields() != TRUE) {
return FALSE;
}
else {
Do a database INSERT, SELECT OR UPDATE etc...
}
}