/ Published in: PHP
A handy, generic form processor which validates presence of fields marked in a hidden value, like: . Easy to override and customize as needed.
Adapted from a script found in PHP 5 Unleashed.
Adapted from a script found in PHP 5 Unleashed.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/** * ============================================== * Generic form processing code * ============================================== */ $form_errorlist = false; /** * Adds an error to our $form_errors variable * * @return void **/ function add_error($error) { global $form_errorlist, $form_errors; $form_errorlist = true; $form_errors[] = $error; } /** * Called by validate_form * @param $method * the container holding our form data $_GET or $_POST * @return void **/ function _process_form($method) { $data = $method; // clean up any elements not included within foreach($data as $key => $val) { } } // call process_form(), pass clean form values process_form($data); } } /** * Generic validation steps * * @return bool **/ function validate_form($method) { $process = true; // assume true until proven otherwise add_error("Required hidden element 'required' missing."); $process = false; } else { // Scan for empties foreach($required_fields as $val) { // Required element is missing data $errormsg = "The required field '" . $method[$val . "_desc"] . "' was empty."; } else { $errormsg = "The required field '$val' was emtpy."; } add_error($errormsg); $process = false; } } // Call dynamic validator methods foreach($method as $key => $val) { $validator_function = $key . "_validate"; // the name of the function which will be called for validation $result = $validator_function($val, $key); } else { $result = $validator_function($val, $method[$key . "_desc"]); } if ($result !== true) { add_error($result); $process = false; } } } } } if ($process) { _process_form($method); return true; } return false; } /** * Returns the value stored in $method or an alternate value when not available * @param $field_name * The name value of the input element * @param $alternate * Text to be displayed if value field_name value is undefined * @return String * @author Nate Miller **/ function write_form_var($field_name, $alternate) { global $method; $out = $alternate; if ($method[$field_name]) { $out = $method[$field_name]; } return $out; } /** * ============================================== * Custom validation logic * ============================================== */ $method = &$_GET; validate_form($method); } /** * Validates an email address * @param $data * the submitted value * @param $desc * the field descriptor - human friendly name of failed field * * @return bool | error string * @author Nate Miller **/ function email_validate($data, $desc) { $email_reg = "/^[a-z0-0\._-]+@+[a-z0-0\.-]+\.+[a-z]{2,3}$/i"; return "The '$desc' field in invalid."; return true; } /** * Controls called by validate_form when form is sucessful * * @return void * @author Nate Miller **/ function process_form() { echo "Submit dude"; }