Location: PHPKode > scripts > Form Builder > form-builder/examples/serializing_form.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"]);

	//Store reference values for pre-filling form fields. 
	$form->setReferenceValues($_POST);
	$_SESSION["reg_form_class"] = 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"]);
	header("Location: serializing_form.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>Serializing Form</b> - This example demonstrates the benefits of serializing a form object instance for form validation and pre-filling form fields. To test the checkForm() function, disable javascript in your browser temporarily.</p>
			<?
			if(!empty($_SESSION["reg_form_class"]))	
			{
				//Instead of rebuilding the form, revive it from the stored shapshot.
				$form = unserialize($_SESSION["reg_form_class"]);
			}	
			else
			{
				$form = new form();
				$form->setAttributes(array(
					"returnUrl" => "serializing_form.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->addButton();

				//Take a snapshot of the object and store in session.
				$_SESSION["reg_form_class"] = 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>Serializing Form</b> - This example demonstrates the benefits of serializing a form object instance for form validation and pre-filling form fields. To test the checkForm() function, disable javascript in your browser temporarily.</p>
			<p>This form was submitted successfully - <a href="serializing_form.php?cmd=">click to go back to form.</a></p>
		</body>
	</html>	
	<?
}
?>
Return current item: Form Builder