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

Support This Project

Using Default Values and Callback Functions

Also try retrieving a record.













Favorite Foods








Favorite Day














Code

<?PHP

//include class, create new Formitable, set primary key field name
include("Formitable.class.php");
$newForm = new Formitable( mysql_connect("server","user","pass"),"db","table" );

$newForm->setPrimaryKey("ID");
$newForm->setEncryptionKey("g00D_3nCr4p7");
    
//retrieve a record for update if GET var set
if( isset($_GET['ID']) ) $newForm->getRecord($_GET['ID']);

//hide primary key field, force a few field types
$newForm->hideField("ID");
$newForm->forceTypes(array("foods","day_of_week"),array("checkbox","radio"));

//get data pairs from another table
$newForm->normalizedField("toon","formitable_toons","ID","name","pkey ASC");

//set custom field labels
$newForm->labelFields( array("f_name","l_name","description","pets","foods","color","day_of_week","b_day","toon"),
                     array(
"First Name","Last Name","About Yourself","Your Pets","Favorite Foods","Favorite Color","Favorite Day","Your Birthday","Favorite Cartoon") );

//set some default values
$newForm->setDefaultValue("color","Blue");
$newForm->setDefaultValue("toon","3");
$newForm->setDefaultValue("pets","Dog,Fish");
$newForm->setDefaultValue("foods","pizza,hamburger");
//force default value even on record retrieval
$newForm->setDefaultValue("day_of_week","Saturday",true);

//set up some call back functions
//value will be set to uppercase on post
$newForm->registerCallback("description", "toUpper");
function
toUpper($key,$value,$args){ return strtoupper($value); }

//value will be set to lowercase on retrieval
$newForm->registerCallback("description", "toLower", "retrieve");
function
toLower($key,$value,$args){ return strtolower($value); }

//callbacks can also be used for field validation
$newForm->registerCallback("f_name", "checkName", "post", "John");
$newForm->registerCallback("l_name", "checkName", "post", "Doe");
function
checkName($key,$value,$args){
    global
$newForm;
    if(
$value!=$args)
        return array(
"status"=>"failed",
            
"errMsg"=>$newForm->getFieldLabel($key)." must be '$args'.");
    
//callback must always return a value
    
else return $value;
}

//output form if not submitted or validation failed
if( !isset($_POST['submit']) ||
    (isset(
$_POST['submit']) && $newForm->submitForm() == -1) ){ $newForm->printForm(); }

?>

MySQL Tables

CREATE TABLE formitable_demo (
  ID tinyint(3) unsigned NOT NULL auto_increment,
  f_name varchar(50) default NULL,
  l_name varchar(50) default NULL,
  description text,
  pets set('Dog','Cat','Fish','Horse','Frog','Rodent','Reptile','None') default NULL,
  foods set('pizza','pasta','salad','sandwich','hamburger') default NULL,
  color enum('Red','Blue','Green','Purple','Yellow','Other') default NULL,
  day_of_week enum('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday') default NULL,
  b_day date default NULL,
  toon tinyint(3) unsigned NOT NULL default '1',
  PRIMARY KEY  (ID)
);


CREATE TABLE formitable_toons (
  ID tinyint(3) unsigned NOT NULL auto_increment,
  name tinytext NOT NULL,
  PRIMARY KEY  (ID)
);

INSERT INTO formitable_toons VALUES (1, 'The Simpsons');
INSERT INTO formitable_toons VALUES (2, 'The Flinstones');
INSERT INTO formitable_toons VALUES (3, 'Dexter\'s Lab');
INSERT INTO formitable_toons VALUES (4, 'Power Puff Girls');
INSERT INTO formitable_toons VALUES (5, 'The Jetsons');
INSERT INTO formitable_toons VALUES (6, 'Family Guy');
INSERT INTO formitable_toons VALUES (7, 'Scooby Doo');
INSERT INTO formitable_toons VALUES (8, 'Other');