// -------------------------------------------------------- // This file shows how to use the AutoForm function. // // It is a function that builds an HTML form based on a // DESCRIBE of a table. It saves lots of time for web // based applications. // // AutoForm is compact and easy to use once you understand // its context. There are more comments than actual code // in this file. The entire form you see in this file is // generated by one function call. // // We will first explain the MySQL table it is // building a form from for this particular example. // -------------------------------------------------------- // MySQL Notes: // // We will assume that we have created a table // // create table eg_table ( // eg_id integer default 0 not null auto_increment, // first_name varchar(255) not null, // initial varchar(50) not null, // last_name varchar(255) not null, // birth_date date not null, // gender enum('M', 'F'), // notes text null, // some_other_id integer default 0 not null, // last_updated timestamp(14) not null, // primary key eg_key(eg_id) // ) // // Such that: // // mysql> desc eg_table; // // +---------------+---------------+------+-----+------------+----------------+ // | Field | Type | Null | Key | Default | Extra | // +---------------+---------------+------+-----+------------+----------------+ // | eg_id | int(11) | | PRI | NULL | auto_increment | // | first_name | varchar(255) | | | | | // | initial | varchar(50) | | | | | // | last_name | varchar(255) | | | | | // | birth_date | date | | | 0000-00-00 | | // | gender | enum('M','F') | YES | | NULL | | // | notes | text | YES | | NULL | | // | some_other_id | int(11) | | | 0 | | // | last_updated | timestamp(14) | YES | | NULL | | // +---------------+---------------+------+-----+------------+----------------+ // 9 rows in set (0.01 sec) // // mysql> // // We will now make a connection to this DB and use the autoform // to generate an HTML form for the above. // -------------------------------------------------------- $db = mysql_connect(); global $db; mysql_select_db('eg_autoform', $db); // -------------------------------------------------------- // Example begins here: // -------------------------------------------------------- include "forms.php"; include "auto_forms.php"; // Define up to 8 variables: // 1) Input $input = $_POST; // default values (and more to be set later) $input['next_action'] = "Save Changes"; // need to define 'next_action' for button // 2) Table name $table = 'eg_table'; // table we're doing a DESC of // 3) By default HTML SelectFields AKA "pull-downs" are used. // You can set it to radio buttons if you like $radio = true; // The rest of the fields are optional, I use them in this example to show // you what AutoForm can do. // 4) Action e.g. "
" $action = $_SERVER['SCRIPT_NAME']; // 5) Explicit Form labels // if you don't like defualt: ucwords(str_replace("_", " ", $field_name)) $explicit_label = array('notes' => 'Special Notes'); // make notes have "Special" // 6) Hidden Fields, i.e. fields that are in the desc that you want to hide $hidden_fields = array('last_name'); // note that last name won't appear // 7) Hidden Variables that you want to set to type hidden in the form (to pass along) // e.g. "" $hidden_vars = array('hidden_field1' => 'foo', 'hidden_field2' => 'bar', ); // 8) Rename the options for the fields with an assoc array of assoc arrays // note: that I'm spelling out the options for 'gender' and adding a new option $choice_fields = array( 'gender' => array( 'M' => 'Male', 'F' => 'Female', 'O' => 'Other' ) ); // $choice_fields = array(); // Call function $form = AutoForm($input, $table, $radio, $action, $explicit_label, $hidden_fields, $hidden_vars, $choice_fields ); print eval($form); // need to eval the output // Note that $form is actually PHP code. We need to eval it // if we want to see the form. // -------------------------------------------------------- // The autoform can also show you what intermediate functions // it called from functions.php so that it can be used as a // starting point for other forms (incase the autoform is too // rigid). // -------------------------------------------------------- print "
\n"; print "AutoForm's output as a code generator for forms.php: \n"; print "

\n"; // we simply don't eval it if we want to show the code: print "

\n";
print $form;
print "
\n"; mysql_close($db); ?>