Online
 
Friday, 09 January 2009
 
 
More article:
Related Content:

Form Processing
 
Article Index
Form Processing
Page 2
Page 3
Page 4
Page 5
Page 6
Page 7
Page 8
Page 9
 
<?php

/********** 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) {

if(preg_match("/(submit|required)|(_desc$)/i", $key) == 1)
unset($data[$key]);
}

/* 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'])) {

add_error("Required hidden element 'required' missing!");
$process = false;
} else {

/* 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. */

if($result !== true) {
add_error($result);
$process = false;
}
}
}
}
}

/* 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 ***********/

?>

Tags: Add more tags...,
This entry was posted on . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a comment. Tags: Simple PHP, Pear, Easy PHP, PHP Tutorial, PHP MySQL, XSLT, Sap Tutorial, CSS Tutorial, XSL FO Java, SQL Tutorial.
Users' Comments (0)

Comment an article
  Name
  E-mail
   Title
Available characters: 4000
 Notify me of follow-up comments
This image contains a scrambled text, it is using a combination of colors, font size, background, angle in order to disallow computer to automate reading. You will have to reproduce it to post on my homepage
Enter what you see:

No comment posted

Cool Graphic Wallpaper 28
Statistic


Last Post

 
Top! Top!