skip navigation

www.Hilands.com


Content:: PHP Form Process Class

PHP - Form Process Class

The Form Process Class takes static and dynamic form files in html format and does simple processing.
Included is a basic captcha security script from
http://www.white-hat-web-design.co.uk/articles/php-captcha.php
The fpc_example.php is an example script that processes the html form using the form_process.class.inc.php file. We activate the session for the mostly functional captcha script.
session_start();
We'll include the form_process.class.inc.php file here. We could use a simple require instead of the include wrapped in an if statement. I prefer the custom messages. You may want to change include to include_once.
if (file_exists("./form_process.class.inc.php")) { include ("./form_process.class.inc.php"); } else { echo 'failed to open form_process.class.inc.php'; exit;}
The configuration is handled inside the $arrFormConf which is an array that will be processed.
formFile points to the html file to be processed.
validReferer needed to be edited if used. This is used for an additional layer of security which requires the referer to be your form. This should stop most attack scripts from randomly abusing your form from an outside source.
fieldText is used to wrap the text near the field to help he user find the field in error. This should be unique on the page. The text1 field has "Text Field 1 (required) :<br />" next to it, the script will search that exact text and will be wrapped with the "errorWrapper" text.
Validation can be required, email and captcha. It will used layered arrays to determine the validation you want on the field. The first array key will be the field name.
errorWrapper is used to wrap the text defined in fieldText
errorMsgWrapper is used to wrap the error message that appears in [errorMsg] section of the html form.
errorMsg section holds custom error messages for the different validation types referer, required, email and captcha.
$arrFormConf = array(
	'formFile' => "fpc_example.tpl.html",
	'validReferer' => array(
		'http:///fpc_example.php'
	),
//	'emailData' => array(
//		'from' => 'emailform@localhost', //replace with input field from form.
//		'to' => 'root@localhost',
//		'cc' => null,
//		'subject' => 'email form',
//		'message' => null
//	),
	'fieldText' => array(
		'text1' => 'Text Field 1 (required) :
', 'text2' => 'Text Field 2 :
', 'name' => 'Name :
', 'textarea' => 'Text Area 1 :
', 'textarea2' => 'Text Area 2 :
', 'select' => 'Select Box 1 :
', 'checkbox1' => 'Check Box 1 : ', 'checkbox2' => 'Check Box 2 : ', 'captcha' => 'Enter text from security image :
', ), 'validation' => array( 'text1' => array( 'required', // 'email', ), 'captcha' => array( 'required', 'captcha', ), ), 'errorWrapper' => array( '0' => "\n".'', '1' => "\n" ), 'errorMsgWrapper' => array( '0' => "\n".'
Error
', '1' => "
\n" ), 'errorMsg' => array( 'referer' => 'Invalid Referer ['.$_SERVER['HTTP_REFERER'].']- You must use the same form provided by the host processing it', 'required' => '[field] is a required field and cannot be blank', // 'email' => 'The Email address entered is not valid please verify', 'captcha' => 'Captcha text does not match the image, use the reset checkbox to reset the image', ) );
Instantiate the class
$refForm = new form_process($arrFormConf);
Check to see if data has been submitted you can use $_GET, $_POST or in this case $_REQUEST to grab both post and get data. This will set the boolean for boolDataSent which will determine how we do the processing.
if (array_key_exists('submit', $_REQUEST))
	$boolDataSent = true;
else
	$boolDataSent = false;
If data was sent to the file we now start our process in the if statement. Our first check runs a few functions from the class checkReferer and checkValidation which run against the configuration array above.
if ($boolDataSent)
{
	// run error checking
	$refForm->checkReferer();
	$refForm->checkValidation();
If errors were found we will process the html file and add the user submitted data with the class function processInputData. and return the form that is stored in strTemplate.
	if ($refForm->returnErrors())
	{
		// process form submission data if errors for form reprint
		$refForm->processInputData();
		echo $refForm->strTemplate;
		#echo "arrFields : 
";print_r($refForm->arrRequest);echo "

\n"; #echo "Session :
";print_r($_SESSION);echo "

\n"; }
If no errors were found we reset the captcha and process the data. Line 179 should be replaced with your post processing routine like submitting the data to a database, email etc.
	else
	{
		$_SESSION['captcha_valid'] = false;
		$_SESSION['captcha_reset'] = true;
		echo "data has been submitted!";
	}
}
If no data has been submitted we simply display the original template file.

else
{
	echo $refForm->strTemplateOrig;
}
#echo "
"; print_r($_REQUEST); echo "
"; #echo "
"; print_r($refForm->arrFields); echo "
";

View Example

form_process.class.inc.20091027.zip
Download 27,585 bytes
8301f164654d60b0f578e7af1b73351c *form_process.class.inc.20091027.zip

Last Modified: 2012-10-21