/********** BEGIN FORM VALIDATION SCRIPT ***********/ $form_errors = array(); $form_errorlist = false;
function add_error($error) { global $form_errorlist, $form_errors; $form_errorlist = true; $form_errors[] = $error; }
function _process_form($method) {
/** This function is called by the validate_form() function only! */
/* Check to see if the process_form() function exists. If this function doesn't exist, there is no need to bother with cleaning up the form data. */ if(function_exists("process_form")) {
/* Make a copy of the submission data and iterate through it removing any elements that aren't part of the actual submission from the copy. */ $data = $method; foreach($data as $key=>$val) {
/* Call the process_form() function and pass it the cleaned up version of the form submission */ process_form($data); } }
function validate_form($method) {
/* This variable is used to determine if any validation errors occurred during the course of the function. By default, we assume the form is valid */ $process = true;
/* Check for the existence of the 'required' form element. If this element does not exist the form is automatically invalid. */ if(!isset($method['required'])) {
/* Parse out the required field elements from the 'required' form element and store them in an array*/ $required = explode(',',$method['required']);
/* Check to ensure each required element exists, and at least has some sort of data (not empty) */ foreach($required as $val) { if(empty($method[$val])) {
/* This particular element should have some data, but for some reason is empty. Hence, attempt to get the human-friendly description of the element and display an error to the user. If no human-friendly description was provided use the element name instead */ if(isset($method[$val."_desc"])) { $errormsg = "The required field '" . $method[$val."_desc"] . "' was empty!"; } else { $errormsg = "The required field '$val' was empty!"; } add_error($errormsg); $process = false; } }
/* Begin the iteration through all of the form elements */ foreach($method as $key=>$val) {
/* Because we are only concerned with validating the actual form elements the user is editing, only check elements that are not named 'submit', 'required' or end in '_desc' */ if(preg_match("/(submit|required)|(_desc$)/i", $key) != 1) {
/* Construct the function name that will be called to validate the data */ $func = $key."_validate";
/* Check to see if the validation function exists for this form element. */ if(function_exists($func)) {
/* Since the validation function exists for this element, call it passing it the value of the element and the human-friendly description (if available) */ if(!isset($method[$key."_desc"])) { $result = $func($val, $key); } else { $result = $func($val, $method[$key."_desc"]); }
/* If the validation function does not return true, then the form element is not valid and $return should contain an error message. Add the error message to the list of errors which occurred. */
/* Assuming no validation errors occurred, $process should still be true. If it is, call the _process_form() function and pass it the validated data and end the function by returning true. */ if($process) { _process_form($method); return true; }
/* Something went wrong in the validation, return false */ return false; }
/********** END FORM VALIDATION SCRIPT ***********/ /********** BEGIN USER-DEFINED SCRIPT ***********/
/* This is just a nicety. By only using $method any time we want to access the superglobal data we can quickly change the submission method from GET to POST (or the other way around) without changing multiple values. */
$method = &$_GET;
/* Check to see if the form was submitted, if so begin the validation process */ if(isset($method['submit'])) { validate_form($method); }
/* This function is called by validate_form() to validate the form element whose name is 'email'. */
function email_validate($data, $desc) { $regex = "/^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,3}$/i"; if(preg_match($regex, $data) != 1) return "The '$desc' field is invalid.";
return true; }
/* This function is called by validate_form() upon successful validation of the form. */ function process_form($data) {
$msg = "The form at {$_SERVER['PHP_SELF']} " . "was submitted with these values: \n\n"; foreach($data as $key=>$val) { $msg .= "$key => $val\n"; } mail("joeuser@somewhere.com", "form submission", $msg);
} /********** END USER-DEFINED SCRIPT ***********/