Location: PHPKode > scripts > PHP Form Processor > php-form-processor/examples/register.php
session_start(); //start the session
require_once('../class.formprocessor.php'); //include the form processor

function checkemailaddress($email) { //this function validates an email address. If it is valid it returns true, otherwise it returns false.
	if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
		return false;
	$email_array = explode("@", $email);
	$local_array = explode(".", $email_array[0]);
	for ($i = 0; $i < sizeof($local_array); $i++) {
		if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
			return false;
	if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) {
		$domain_array = explode(".", $email_array[1]);
		if (sizeof($domain_array) < 2) {
			return false;
		for ($i = 0; $i < sizeof($domain_array); $i++) {
			if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
				return false;
	return true;

function myform() {
$form = <<<EOD
<form id="register" action="register.php" method="post">


<table style="border:none" class="nobor">
<tr><td colspan="2" class="bold">Title</td></tr>
<tr><td colspan="2">
<element id="title">
<select class="selecto" />
<option value="mr." text="Mr." />
<option value="mrs." text="Mrs." />
<option value="miss" text="Miss" />
<option value="ms." text="Ms." />
<option value="dr." text="Dr." />
<option value="prof." text="Prof." />
<error> <strong class="error">!</strong></error>

<tr class="bold"><td>First name</td><td>Last name</td></tr>
<tr><td><element id="fn">
    <input class="inputo" size="30" type="text" />
	<compulsory message="You must enter your first name" />
	<regexp test="|^[a-zA-Z-]*$|" message="Your first name must only contain letters and hyphens" />
	<error> <strong class="error">!</strong></error>

<td><element id="ln">
    <input class="inputo" size="30" type="text" />
	<compulsory message="You must enter your last name" />
	<regexp test="|^[a-zA-Z-]*$|" message="Your last name must only contain letters and hyphens" />
	<error> <strong class="error">!</strong></error>

<td colspan="2" class="bold">Building number or name and street name</td></tr>

<td colspan="2"><element id="add1">
    <input class="inputo" size="30" type="text" />
	<compulsory message="You must enter your building number or name and street name" />
	<error> <strong class="error">!</strong></error>

<td colspan="2" class="bold">Apartment, suite, unit, floor, etc (optional)</td></tr>

<td colspan="2"><element id="add2">
    <input class="inputo" size="30" type="text" />
	<error> <strong class="error">!</strong></error>

<td colspan="2" class="bold">Town/City</td></tr>

<td colspan="2"><element id="tc">
    <input class="inputo" size="30" type="text" />
	<compulsory message="You must enter your town/city" />
	<error> <strong class="error">!</strong></error>

<tr class="bold">

<td><element id="cs">
    <input class="inputo" size="30" type="text" />
	<compulsory message="You must enter your county/state" />
	<error> <strong class="error">!</strong></error>

<td><element id="pc">
<input class="inputo" size="10" type="text" />
<compulsory message="You must enter your postcode/zipcode" />
<maxLength length="7" message="Your postcode/zipcode can't be longer than 7 characters" />
<text transform="upper" />
<error> <strong class="error">!</strong></error>

<td colspan="2" class="bold">Country</td></tr>

<td colspan="2"><element id="co">
<select />
<compulsory message="You must choose your country" />
<option value="AF" text="Afghanistan" />
<option value="AL" text="Albania" />
<option value="DZ" text="Algeria" />
<option value="AS" text="American Somoa" />
<option value="AD" text="Andorra" />
<option value="AO" text="Angola" />
<option value="AI" text="Anguilla" />
<option value="AQ" text="Antartica" />
<option value="AG" text="Antigua and Barbuda" />
<option value="AR" text="Argentina" />
<option value="AM" text="Armenia" />
<option value="AW" text="Aruba" />
<option value="AU" text="Australia" />
<option value="AT" text="Austria" />
<option value="AZ" text="Azerbaijan" />
<option value="BS" text="Bahamas" />
<option value="BH" text="Bahrain" />
<option value="BD" text="Bangladesh" />
<option value="BB" text="Barbados" />
<option value="BY" text="Belarus" />
<option value="BE" text="Belgium" />
<option value="BZ" text="Belize" />
<option value="BJ" text="Benin" />
<option value="BM" text="Bermuda" />
<option value="BT" text="Bhutan" />
<option value="BO" text="Bolivia" />
<option value="BA" text="Bosnia and Herzegovina" />
<option value="BW" text="Botswana" />
<option value="BR" text="Brazil" />
<option value="IO" text="British Indian Ocean Territory" />
<option value="BN" text="Brunei" />
<option value="BG" text="Bulgaria" />
<option value="BF" text="Burkina Faso" />
<option value="BI" text="Burundi" />
<option value="KH" text="Cambodia" />
<option value="CM" text="Cameroon" />
<option value="CA" text="Canada" />
<option value="CV" text="Cape Verde" />
<option value="KY" text="Cayman Islands" />
<option value="CF" text="Central African Republic" />
<option value="TD" text="Chad" />
<option value="CL" text="Chile" />
<option value="CN" text="China" />
<option value="CX" text="Christmas Islands" />
<option value="CC" text="Cocos (Keeling) Islands" />
<option value="CO" text="Colombia" />
<option value="KM" text="Comoros" />
<option value="CG" text="Congo" />
<option value="CD" text="Congo, Democratic Republic of" />
<option value="CK" text="Cook Island" />
<option value="CR" text="Costa Rica" />
<option value="CI" text="Cote Divoire" />
<option value="HR" text="Croatia" />
<option value="CU" text="Cuba" />
<option value="CY" text="Cyprus" />
<option value="CZ" text="Czech Republic" />
<option value="DK" text="Denmark" />
<option value="DJ" text="Djibouti" />
<option value="DM" text="Dominica" />
<option value="DO" text="Dominican Republic" />
<option value="TP" text="East Timor" />
<option value="EG" text="Egypt" />
<option value="SV" text="El Salvador" />
<option value="EC" text="Equador" />
<option value="GC" text="Equatorial Guinea" />
<option value="ER" text="Eritrea" />
<option value="EE" text="Estonia" />
<option value="ET" text="Ethiopia" />
<option value="FK" text="Falkland Islands" />
<option value="FO" text="Faroe Islands" />
<option value="FM" text="Federated States of Micronesia" />
<option value="FJ" text="Fiji" />
<option value="FI" text="Finland" />
<option value="FR" text="France" />
<option value="GF" text="French Guiana" />
<option value="PF" text="French Polynesia" />
<option value="TF" text="French Southern Territories" />
<option value="GA" text="Gabon" />
<option value="GM" text="Gambia" />
<option value="GE" text="Georgia" />
<option value="DE" text="Germany" />
<option value="GH" text="Ghana" />
<option value="GI" text="Gibraltar" />
<option value="GR" text="Greece" />
<option value="GL" text="Greenland" />
<option value="GD" text="Grenada" />
<option value="GP" text="Guadeloupe" />
<option value="GU" text="Guam" />
<option value="GT" text="Guatemala" />
<option value="GN" text="Guinea" />
<option value="GW" text="Guinea-Bissau" />
<option value="GY" text="Guyana" />
<option value="HT" text="Haiti" />
<option value="HM" text="Heard and Macdonald Islands" />
<option value="HN" text="Honduras" />
<option value="HK" text="Hong Kong" />
<option value="HU" text="Hungary" />
<option value="IS" text="Iceland" />
<option value="IN" text="India" />
<option value="ID" text="Indonesia" />
<option value="IR" text="Iran" />
<option value="IQ" text="Iraq" />
<option value="IE" text="Ireland" />
<option value="IL" text="Israel" />
<option value="IT" text="Italy" />
<option value="JM" text="Jamaica" />
<option value="JP" text="Japan" />
<option value="JO" text="Jordan" />
<option value="KZ" text="Kazakhstan" />
<option value="KE" text="Kenya" />
<option value="KI" text="Kiribati" />
<option value="KP" text="Korea, North" />
<option value="KR" text="Korea, South" />
<option value="KW" text="Kuwait" />
<option value="KG" text="Kyrgyzstan" />
<option value="LA" text="Laos" />
<option value="LV" text="Latvia" />
<option value="LB" text="Lebanon" />
<option value="LS" text="Lesotho" />
<option value="LR" text="Liberia" />
<option value="LY" text="Libya" />
<option value="LI" text="Liechtenstein" />
<option value="LT" text="Lithuania" />
<option value="LU" text="Luxembourg" />
<option value="MO" text="Macau" />
<option value="MK" text="Macedonia (Rep. of Fmr Yugoslav)" />
<option value="MG" text="Madagascar" />
<option value="MW" text="Malawi" />
<option value="MY" text="Malaysia" />
<option value="MV" text="Maldives" />
<option value="ML" text="Mali" />
<option value="MT" text="Malta" />
<option value="MH" text="Marshall Islands" />
<option value="MQ" text="Martinique" />
<option value="MR" text="Mauritania" />
<option value="MU" text="Mauritius" />
<option value="YT" text="Mayotte" />
<option value="FX" text="Metropolitan France" />
<option value="MX" text="Mexico" />
<option value="MD" text="Moldova" />
<option value="MC" text="Monaco" />
<option value="MN" text="Mongolia" />
<option value="ME" text="Montenegro" />
<option value="MS" text="Montserrat" />
<option value="MA" text="Morocco" />
<option value="MZ" text="Mozambique" />
<option value="MM" text="Myanmar" />
<option value="NA" text="Namibia" />
<option value="NR" text="Nauru" />
<option value="NP" text="Nepal" />
<option value="NL" text="Netherlands" />
<option value="AN" text="Netherlands Antilles" />
<option value="NC" text="New Caledonia" />
<option value="NZ" text="New Zealand" />
<option value="NI" text="Nicaragua" />
<option value="NU" text="Nieu" />
<option value="NE" text="Niger" />
<option value="NG" text="Nigeria" />
<option value="NF" text="Norfolk Island" />
<option value="MP" text="Northern Mariana Islands" />
<option value="NO" text="Norway" />
<option value="OM" text="Oman" />
<option value="PK" text="Pakistan" />
<option value="PW" text="Palau" />
<option value="PS" text="Palestinian Territory, Occupied" />
<option value="PA" text="Panama" />
<option value="PG" text="Papua New Guinea" />
<option value="PY" text="Paraguay" />
<option value="PE" text="Peru" />
<option value="PH" text="Philippines" />
<option value="PN" text="Pitcairn" />
<option value="PL" text="Poland" />
<option value="PT" text="Portugal" />
<option value="PR" text="Puerto Rico" />
<option value="QA" text="Qatar" />
<option value="RE" text="Reunion" />
<option value="RO" text="Romania" />
<option value="RU" text="Russia" />
<option value="RW" text="Rwanda" />
<option value="GS" text="S. Georgia and S. Sandwich Islands" />
<option value="WS" text="Samoa" />
<option value="SM" text="San Marino" />
<option value="ST" text="Sao Tome and Principe" />
<option value="SA" text="Saudi Arabia" />
<option value="SN" text="Senegal" />
<option value="RS" text="Serbia, Republic of" />
<option value="SC" text="Seychelles" />
<option value="SL" text="Sierra Leone" />
<option value="SG" text="Singapore" />
<option value="SK" text="Slovakia" />
<option value="SI" text="Slovenia" />
<option value="SB" text="Solomon Islands" />
<option value="SO" text="Somalia" />
<option value="ZA" text="South Africa" />
<option value="ES" text="Spain" />
<option value="LK" text="Sri Lanka" />
<option value="SH" text="St Helena" />
<option value="KN" text="St Kitts and Nevis" />
<option value="LC" text="St Lucia" />
<option value="PM" text="St Pierre and Miquelon" />
<option value="VC" text="St Vincent and the Grenadines" />
<option value="SD" text="Sudan" />
<option value="SR" text="Suriname" />
<option value="SJ" text="Svalbard and Jan Mayen Islands" />
<option value="SZ" text="Swaziland" />
<option value="SE" text="Sweden" />
<option value="CH" text="Switzerland" />
<option value="SY" text="Syria" />
<option value="TW" text="Taiwan" />
<option value="TJ" text="Tajikistan" />
<option value="TZ" text="Tanzania" />
<option value="TH" text="Thailand" />
<option value="TG" text="Togo" />
<option value="TK" text="Tokelau" />
<option value="TO" text="Tonga" />
<option value="TT" text="Trinidad and Tobago" />
<option value="TN" text="Tunisia" />
<option value="TR" text="Turkey" />
<option value="TM" text="Turkmenistan" />
<option value="TC" text="Turks and Caicos Islands" />
<option value="TV" text="Tuvalu" />
<option value="UG" text="Uganda" />
<option value="UA" text="Ukraine" />
<option value="AE" text="United Arab Emirates" />
<option value="GB" text="United Kingdom" />
<option value="US" text="United States" />
<option value="UM" text="United States Minor Outlying Islands" />
<option value="UY" text="Uruguay" />
<option value="UZ" text="Uzbekistan" />
<option value="VU" text="Vanuatu" />
<option value="VA" text="Vatican City" />
<option value="VE" text="Venezuela" />
<option value="VN" text="Vietnam" />
<option value="VG" text="Virgin Islands - British" />
<option value="VI" text="Virgin Islands - US" />
<option value="WF" text="Wallis and Futuna Islands" />
<option value="EH" text="Western Sahura" />
<option value="YE" text="Yemen" />
<option value="YU" text="Yugoslavia" />
<option value="ZR" text="Zaire" />
<option value="ZM" text="Zambia" />
<option value="ZW" text="Zimbabwe" />

<default value="GB" />
<error> <strong class="error">!</strong></error>

<td colspan="2" class="bold">Phone</td></tr>
<td colspan="2"><element id="ph_a">
    <input class="inputo" size="7" type="text" maxlength="6" />
	<compulsory message="You must enter your phone number area code" />
	<validate test="numeric" message="The area code of phone number must be numerical" />
	<error> <strong class="error">!</strong></error>
</element> <element id="ph_p">
    <input class="inputo" size="12" type="text" maxlength="8" />
	<compulsory message="You must enter your phone number" />
	<validate test="numeric" message="Your phone number must be numerical" />
	<error> <strong class="error">!</strong></error>

<tr><td colspan="2" class="bold">Email address</td></tr>
<tr><td colspan="2"><element id="email">
    <input class="inputo" size="30" type="text" />
	<compulsory message="You must enter your email address" />
	<callback function="checkemailaddress" message="The email address you entered is invalid" />
	<text transform="lower" />
	<error> <strong class="error">!</strong></error>

<element id="registerbtn">
<input type="submit" value="Register" class="submito" />

return $form;

try {
	$processor =& new FormProcessor(myform()); //create a new instance of the form processor
	if(isset($_POST["registerbtn"])) { //if the form has been submitted
		if ($processor->validate()) { //if the form validated
			echo '<h1>Success!</h1>';
			print_r($_POST); //print the form variables
    		$processor->display(); //print the form
		$processor->display(); //print the form
catch(Exception $e) { //an error occured
	echo $e->getMessage(); //print the error message
Return current item: PHP Form Processor