skip navigation

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
The fpc_example.php is an example script that processes the html form using the file. We activate the session for the mostly functional captcha script.
We'll include the 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("./")) { include ("./"); } else { echo 'failed to open'; 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(
//	'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".'
', '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;
	$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
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
		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.
		$_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.

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

View Example
Download 27,585 bytes
8301f164654d60b0f578e7af1b73351c *

Last Modified: 2012-10-21