Location: PHPKode > scripts > Form Builder > form-builder/examples/form_validation.php
<?
/*Include Form Class*/
include("../class.form.php");
session_start();

if(!isset($cmd))
	$cmd = "";

if($cmd == "submit")
{
	//Revive form object from stored snapshot.
	$form = unserialize($_SESSION["reg_form_class_2"]);

	//Store reference values for pre-filling form fields. 
	$form->setReferenceValues($_POST);
	$_SESSION["reg_form_class_2"] = serialize($form);

	//Cycles through required fields and validates.  This function will redirect and exit on error.
	$form->checkForm();

	//Getting to this point mean that the form has been validated.  We now unset the stored snapshot.
	unset($_SESSION["reg_form_class_2"]);
	header("Location: form_validation.php?cmd=success");
	exit();
}

if(!empty($_GET["error_message"]))
	echo("<div style='text-align: center;'>" . $_GET["error_message"] . "</div>");

if($cmd == "")
{
	?>
	<html>
		<head></head>
		<body>
			<h2 style="text-align: center; margin: 0; padding: 0;">Form Builder Class - Examples</h2>
			<h5 style="text-align: center; margin: 0; padding: 0;"><span style="padding-right: 10px;">Author: Andrew Porterfield</span><span style="padding-right: 10px;">Released: April 24, 2009</span><span>Version: 0.4.0<span></h5>
			<a href="../index.html">Back to Project Home Page</a>
			<p><b>Form Validation</b> - This form class provides form validation through both javascript and PHP. For javascript validation, simply set the "required" parameter to 1 and an onsubmit function for validation will automatically be generated. For PHP validation, utility serialization to take a snap shot of your form before submission, and then revive it after submission by using the unserialize() built in php function.  Once restored, call the checkForm function, which cycles through your required fields and validates. To test the checkForm() function, disable javascript in your browser temporarily so you can skip the javascript validation process.</p>
			<?
			if(!empty($_SESSION["reg_form_class_2"]))	
			{
				//Instead of rebuilding the form, revive it from the stored shapshot.
				$form = unserialize($_SESSION["reg_form_class_2"]);
			}	
			else
			{
				$form = new form();
				$form->setAttributes(array(
					"jqueryPath" => "../jquery",
					"returnUrl" => "form_validation.php?cmd=",
					"tableAttributes" => array("width" => "300")
				));

				$form->addHidden("cmd", "submit");
				$form->addTextbox("Required Textbox:", "field0", "", array("required" => 1));
				$form->addSelectbox("Required Selectbox:", "field1", "", array("" => "--Select an Option--", "option1" => "Option 1", "option2" => "Option 2"), array("required" => 1));
				$form->addCheckbox("Required Checkbox:", "field2", "", array("option1" => "Option 1"), array("required" => 1));
				$form->addDate("Date:", "field3", "", array("required" => 1));
				$form->addButton();

				//Take a snapshot of the object and store in session.
				$_SESSION["reg_form_class_2"] = serialize($form);
			}	
			$form->render();
			?>
			<a href="../index.html">Back to Project Home Page</a>
		</body>	
	</html>	
	<?
}
elseif($cmd == "success")
{
	?>
	<html>
		<head></head>
		<body>
			<h2 style="text-align: center; margin: 0; padding: 0;">Form Builder Class - Examples</h2>
			<h5 style="text-align: center; margin: 0; padding: 0;"><span style="padding-right: 10px;">Author: Andrew Porterfield</span><span style="padding-right: 10px;">Released: April 24, 2009</span><span>Version: 0.4.0<span></h5>
			<a href="../index.html">Back to Project Home Page</a>
			<p><b>Form Validation</b> - This form class provides form validation through both javascript and PHP. For javascript validation, simply set the "required" parameter to 1 and an onsubmit function for validation will automatically be generated. For PHP validation, utility serialization to take a snap shot of your form before submission, and then revive it after submission by using the unserialize() built in php function.  Once restored, call the checkForm function, which cycles through your required fields and validates. To test the checkForm() function, disable javascript in your browser temporarily so you can skip the javascript validation process.</p>
			<p>This form was submitted successfully - <a href="form_validation.php?cmd=">click to go back to form.</a></p>
			<a href="../index.html">Back to Project Home Page</a>
		</body>	
	</html>	
	<?
}

?>
Return current item: Form Builder