<? /* Example file using Formslib4PHP Small form with simple database update access Requires: MySQL database */
// Using the library require( '../formslib.php' );
// Opening an ADODB connection $conn = ADONewConnection( 'mysqli' ); // Use this for mysql, since DataBlack wants transaction emulation $conn->Connect( 'localhost', 'root', false, 'test' );
// Defining the form and our fields $form = new HTMLForm('myform'); $field = new HTMLFormInput( 'text', 'pers_name', 'Name:' ); $f = new HTMLFormControl( 'submitbutton', 'Update!', 'submit' );
// Creating the data block. The ADODB connection $conn must exist at this point! // Note, that we don't need to initialize this Formslib block manually with ::init(), // because we are going to use it in form mapping and the form object will do it for us! $block = new DataBlock('myblock', 'SELECT id, name FROM person WHERE id=2', DATA_DB, $conn,' person', 'id');
// Let's add our defined blocks and fields to the form $form->add_fields(); $form->add_blocks();
// We set a very simple template for the form $tpl = "%pers_name[LABEL]%: %pers_name[FIELD]% %submitbutton[FIELD]% "; $form->set_template( $tpl );
// Here we map our field 'pers_name' to the data block recordset column 'name'. // From now, the field 'pers_name' will automatically be filled with the value of the column 'name' // from the table 'persons_table' on FORM_INIT and updated with the form value of this field // on FORM_SUBMIT! Easy, what? $form->map( 'myblock', array( 'pers_name'=>'name' ) );
// Doing the main stuff $form->setup();
// ...and output. print $form->out();
// The <form>_success trigger // Note the naming convention for formslib triggers! function _myform_success() { // Simply uncomment the following line if you want to display another page // instead of the form after submitting the data. // FLIB::HTML('myform')->set_template( 'Thank you for updating!' ); } ?>
|