n. 1. the inherit form in every MySQL table. 2. the php script that makes it happen.

Support This Project

Multiple Page User Registration Example

Page 1 of 3





























Code

<?php
//include class, set username, password, database, and server vars
require("Formitable.class.php");

$user = "name";
$pass = "secret";
$DB = "formitable";
$server="localhost";

//create new Formitable, args are mysql connection, database and table
$newForm = new Formitable( @mysql_connect($host,$user,$pass),$DB,"formitable_users" );

//set the field name of primary key
$newForm->setPrimaryKey("UserID");

//set an encryption key so the record ID is encrypted to prevent tampering
$newForm->setEncryptionKey("g00D_3nCr4p7");

//retrieve record if get ID or post pkeyID
if( isset($_GET['ID']) ) $newForm->getRecord($_GET['ID']);
else if( isset(
$_POST['pkey']) ){
    
$newForm->getRecord( $_POST['pkey'], isset($newForm->rc4key) );
}

//force a few field types
$newForm->forceTypes( array("UserType","WkDays","Occupation","Address","Address2","City","Password"),
                      array(
"select","checkbox","text","text","text","text","password") );

//retrieve normalized data from another table
$newForm->normalizedField("UserType","field_data","ID","name","ID ASC","type='user'");
$newForm->normalizedField("NewsLetter","field_data","ID","name","ID DESC","type='yn'");
$newForm->normalizedField("Donation","field_data","ID","name","ID ASC","type='donate'");
$newForm->normalizedField("recurring_Method","field_data","ID","name","ID ASC","type='recurring'");
$newForm->normalizedField("Donation_Type","field_data","ID","name","ID ASC","type='dtype'");
$newForm->normalizedField("NamePosted","field_data","ID","name","ID DESC","type='yn'");
$newForm->normalizedField("volunteer","field_data","ID","name","ID ASC","type='volunteer'");
$newForm->normalizedField("gender","field_data","ID","name","ID ASC","type='gender'");
$newForm->normalizedField("WkDays","field_data","ID","name","ID ASC","type='days'");
$newForm->normalizedField("StateCode","states","Code","Name","Name ASC");

//set custom field labels
$newForm->labelFields( array("FName","MName","LName","UserType","FindUs","NewsLetter","volunteer","Donation","NamePosted",
      "WkDays","PostalCode","StateCode","TelePhone","Mobile","Facsimile","Details"),
array(
"First Name","Middle Name","Last Name","I am a","How did you find us?",
      "Subscribe to our newsletter?", "I want to Volunteer", "I would like to donate",
    
  "Add name to Sponsors page?", "I can work these days","Zip Code","State","Home Phone",
      "Cell Phone","FAX","Personal Message")
);
$newForm->labelField("Password_verify","Verify Password");

//don't output field sets (<fieldset> tag)
$newForm->fieldSets=false;

//set up regular expressions for field validation
$newForm->registerValidation("required",".+","Input is required.");
$newForm->registerValidation("valid_email",'^[a-zA-Z0-9_]{2,50}@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.?]+$',
        
"Invalid email address.");
$newForm->registerValidation("uspostal","^[0-9]{5}(-?[0-9]{4})?$","Invalid US Postal Code.");
$newForm->registerValidation("currency_us","^([0-9]+(\.[0-9]{2})?)?$","Use dollar amount only.");
$newForm->registerValidation("six_chars",".{6,}","Enter at least six characters.");

//set up fields for validation using regexs above
$newForm->validateField("FName","required");
$newForm->validateField("Email","valid_email");
$newForm->validateField("Donation_Amount","currency_us");
$newForm->validateField("Password","six_chars");

//require the email field to be unique in the database (doesn't already exist)
$newForm->uniqueField("Email","Email is already registered.");

//set custom success message for update (after last page)
$newForm->msg_updateSuccess="<center><div style=\"width:455; padding:15px; background-color:#F1F3F3;\">
<p>Registration is now complete. Thanks for joining!</p></div></center>"
;

//output a feedback box at the top, and a line above each invalid field
$newForm
->feedback="both";

//test for last page and no errors to submit form, otherwise start form
    if( @$_POST['formitable_multipage']!="end" || isset($newForm->errMsg) ) $newForm->openForm();
    else $newForm->submitForm();

//first page - test for no submit OR errors set with a field on the first page
    if( !isset($_POST['submit']) || (isset($newForm->errMsg) && isset($_POST['FName'])) ):
    /*** open form pg 1 of 3 ***/ ?>
<h3>Page 1 of 3</h3>
<table align="center">
<tr> <!-- valign bottom to stay aligned when there is an error message. -->
<td width=225 valign="bottom"><?php $newForm->printField("FName"); ?></td>
<td width=225 valign="bottom"><?php $newForm->printField("MName"); ?></td>
<td width=225 valign="bottom"><?php $newForm->printField("LName"); ?></td>
</tr>
<tr>
<td width=225 valign="bottom"><?php $newForm->printField("Email"); ?></td>
<td width=225 valign="bottom"><?php $newForm->printField("Password"); ?></td>
<td width=225 valign="bottom"><?php $newForm->printField("Password","",true); ?></td>
</tr>
<tr>
<td width=225><?php $newForm->printField("UserType"); ?></td>
<td width=225><?php $newForm->printField("Age"); ?></td>
<td width=225><?php $newForm->printField("gender"); ?></td>
</tr>
</table>

<?php //on the first page set multipage to "start"
    
$newForm->multiPage("start");
    $newForm->closeForm(); ?>

<?php //test for a field on the previous page OR errors set with a field from this page
    
elseif( isset($_POST['FName'] ) || ( isset($newForm->errMsg) && isset($_POST['City']) ) ):
    /*** open form pg 2 of 3 ***/ ?>
<h3>Page 2 of 3</h3>
<table align="center">
<tr>
<td width=225><?php $newForm->printField("Occupation"); ?></td>
<td width=225><?php $newForm->printField("Address"); ?></td>
<td width=225><?php $newForm->printField("Address2"); ?></td>
</tr>
<tr>
<td><?php $newForm->printField("City"); ?></td>
<td><?php $newForm->printField("StateCode"); ?></td>
<td><?php $newForm->printField("PostalCode"); ?></td>
</tr>
<tr>
<td><?php $newForm->printField("TelePhone"); ?></td>
<td><?php $newForm->printField("Mobile"); ?></td>
<td><?php $newForm->printField("Facsimile"); ?></td>
</tr>
</table>

<?php //on the intermediate pages set multipage to "next"
    
$newForm->multiPage("next");
    $newForm->closeForm(); ?>

<?php //test for a field on the previous page OR errors set with a field from this page
    elseif( isset($_POST['City']) || (isset($newForm->errMsg) && isset($_POST['Donation'])) ):
    /*** open form pg 3 of 3 ***/ ?>
<h3>Page 3 of 3</h3>
<table align="center">
<tr>
<td width=225 valign="top"><?php $newForm->printField("FindUs"); ?></td>
<td width=225 valign="top"><?php $newForm->printField("NewsLetter"); ?></td>
<td width=225 valign="top"><?php $newForm->printField("volunteer"); ?></td>
</tr>
<tr>
<td width=450 height=100% colspan=2><?php $newForm->printField("Details"); ?></td>
<td width=225 valign="top"><?php $newForm->printField("WkDays"); ?></td>
</tr>
</table>

<table align="center">
<tr>
<td width=225 valign="bottom"><?php $newForm->printField("Donation"); ?></td>
<td width=225 valign="bottom"><?php $newForm->printField("Donation_Amount"); ?></td>
</tr>
</table>
<table align="center">
<tr>
<td width=225 valign="top"><?php $newForm->printField("Donation_Type"); ?></td>
<td width=225 valign="top"><?php $newForm->printField("recurring_Method"); ?></td>
</tr>
</table>

<?php //on the last page set multipage to "end"
    
$newForm->multiPage("end");
    $newForm->closeForm(); ?>

<?php endif; ?>


MySQL Tables

CREATE TABLE field_data (
  ID tinyint(3) unsigned NOT NULL default '0',
  name tinytext NOT NULL,
  type varchar(10) NOT NULL default ''
) TYPE=MyISAM;

INSERT INTO field_data VALUES (0, 'Please make a selection', 'user');
INSERT INTO field_data VALUES (1, 'Student', 'user');
INSERT INTO field_data VALUES (2, 'Developer', 'user');
INSERT INTO field_data VALUES (3, 'Management', 'user');
INSERT INTO field_data VALUES (1, 'Yes', 'yn');
INSERT INTO field_data VALUES (0, 'No', 'yn');
INSERT INTO field_data VALUES (0, 'Make a Selection', 'donate');
INSERT INTO field_data VALUES (1, 'I would like to pay by credit card', 'donate');
INSERT INTO field_data VALUES (2, 'I would like to pay by check', 'donate');
INSERT INTO field_data VALUES (4, 'I am requesting donation forms', '_donate');
INSERT INTO field_data VALUES (5, 'Please send me information', '_donate');
INSERT INTO field_data VALUES (1, 'One Time payment', 'dtype');
INSERT INTO field_data VALUES (6, 'Not at this time', 'donate');
INSERT INTO field_data VALUES (2, 'Recurring payment', 'dtype');
INSERT INTO field_data VALUES (1, 'Monthly', 'recurring');
INSERT INTO field_data VALUES (2, 'Quarterly', 'recurring');
INSERT INTO field_data VALUES (3, 'Semi-annually', 'recurring');
INSERT INTO field_data VALUES (4, 'Annually', 'recurring');
INSERT INTO field_data VALUES (0, 'Make a Selection', 'volunteer');
INSERT INTO field_data VALUES (1, 'Yes', 'volunteer');
INSERT INTO field_data VALUES (2, 'No', 'volunteer');
INSERT INTO field_data VALUES (1, 'Female', 'gender');
INSERT INTO field_data VALUES (2, 'Male', 'gender');
INSERT INTO field_data VALUES (1, 'Monday', 'days');
INSERT INTO field_data VALUES (2, 'Tuesday', 'days');
INSERT INTO field_data VALUES (3, 'Wednesday', 'days');
INSERT INTO field_data VALUES (4, 'Thursday', 'days');
INSERT INTO field_data VALUES (5, 'Friday', 'days');
INSERT INTO field_data VALUES (6, 'Saturday', 'days');
INSERT INTO field_data VALUES (7, 'Sunday', 'days');


CREATE TABLE formitable_users (
  UserID int(5) unsigned NOT NULL auto_increment,
  Email varchar(40) NOT NULL default '',
  Password varchar(16) NOT NULL default '',
  FName varchar(25) NOT NULL default '',
  MName varchar(25) NOT NULL default '',
  LName varchar(25) NOT NULL default '',
  UserType enum('0','1','2','3','4') NOT NULL default '0',
  Occupation tinytext NOT NULL,
  Address tinytext NOT NULL,
  Address2 tinytext NOT NULL,
  City tinytext NOT NULL,
  PostalCode int(5) unsigned NOT NULL default '0',
  TelePhone varchar(16) NOT NULL default '',
  Mobile varchar(16) NOT NULL default '',
  Facsimile varchar(16) NOT NULL default '',
  FindUs enum('Website','Search','Word of Mouth','Other') NOT NULL default 'Search',
  NewsLetter enum('1','0') NOT NULL default '1',
  Donation enum('0','1','2','3','4','5','6') NOT NULL default '0',
  Donation_Type enum('0','1','2') NOT NULL default '0',
  Donation_Amount varchar(7) NOT NULL default '0',
  recurring_Method enum('0','1','2','3','4','5','6') NOT NULL default '0',
  NamePosted enum('1','0') NOT NULL default '1',
  volunteer enum('2','1','0') NOT NULL default '0',
  gender enum('2','1','0') NOT NULL default '0',
  Age tinyint(3) unsigned NOT NULL default '0',
  WkDays set('','1','2','3','4','5','6','7') NOT NULL default '',
  Details tinytext NOT NULL,
  StateCode enum('','AK','AZ','AR','CA','CO','CT','DE','FL','GA','GU','HI','ID','IL','IN','IA','KS','KY',
      'LA','ME','MH','MD','MA','MI','MN','MS','MO','MT','NE','NV','NJ','NM','NY','NC','ND','OH','OK','OR',
      'PW','PA','PR','RI','SC','SD','TN','TX','UT','VT','VI','VA','WA','WV','WI','WY') NOT NULL default '',
  CancelDonation enum('1','0') NOT NULL default '0',
  PRIMARY KEY  (UserID,UserID)
) TYPE=MyISAM;


# also see `states` table in survey style example