<? 
/* 
Example file using Formslib4PHP 
More dml operations (insert, update) using a dropdown as navigation 
Requires: MySQL database 
*/ 
 
require( '../formslib.php' ); 
$oDB = ADONewConnection( 'mysqli' ); 
$oDB->Connect( 'localhost', 'root', false, 'test' ); 
 
// Doing some security stuff 
$id = (int)$oDB->GetOne( 'SELECT id FROM person WHERE id='.(int)$_REQUEST['id'] ); 
 
// Setting up the data block 
$oBlock = new DataBlock( 'myblock', "SELECT * FROM person where id = $id", DATA_DB, $oDB, 'person', 'id' ); 
 
// This will be our template. 
$template = " 
<html> 
    <title>A test form</title> 
    <body> 
        <center>%_form[TITLE]%</center> 
        <center> 
        %sometext[FIELD]%<br /> 
        %id[LABEL]%: %id[FIELD]% <br />     
        %hr1[FIELD]% 
        %now[LABEL]%: %now[FIELD]% <br /><br /> 
        %age[LABEL]%: %age[FIELD]%%age[ERROR]% <br /> 
        %name[LABEL]%: %name[FIELD]%%name[ERROR]% <br /> 
        %memo[LABEL]%: %memo[FIELD]% <br /><br /> 
        %save[FIELD]% 
        %hr1[FIELD]% 
    </center> 
    </body> 
</html> 
"; 
 
// Let's create the main form object. 
// You can enter the form title as the second construction argument, or simply use the method 
//  ::set_title() after construction 
$form = new HTMLForm( 'test' ); 
$form -> set_title('Persons'); 
 
// Adding some controller for different behaviour when we're just navigating 
$form->add_control( 'navigation', true, FORM_INIT, 'GET' ); 
 
// Defining some fields and setting properties and validators for our example 
 
// We just use a dropdown for navigation 
$f = new HTMLFormList( 'dropdown', 'id', 'Records', $id ); 
$f->populate( 'SELECT id, name FROM person', false, $oDB ); 
$f->set_property( 'empty', array( 'new'=>'--- Insert new record ---' ) ); 
$f->add_attr( array ( 'onchange'=>"javascript:document.test.action='?navigation=1'; document.test.submit();" ) ); 
$f->set_property ( 'readonly' ); 
 
// A database field with simply internal validation 
// The validation will only occur if the field ist not empty, because it's not defined as required. 
$f = new HTMLFormInput( 'text', 'age', 'Age' ); 
$f->add_validation( 'int_abs', 'Please enter a number greater than zero!', V_INTERNAL ); 
 
// A normal text input field which we'll map to the data block 
// We assign 2 validations to this field, an internal and an user specific 
$f = new HTMLFormInput( 'text', 'name', 'Full name' ); 
$f->set_property( 'required', '"%LABEL%" is a required field!' ); 
$f->add_validation('personal_name', '"%LABEL%" contains disallowed characters.', V_INTERNAL); 
$f->add_validation('my_validate_function', 'The first letter should be uppercase.', V_USER); // Just an example 
 
// A textarea field. Note the extra added HTML attributes 
$f = new HTMLFormInput( 'textarea', 'memo', 'Description' ); 
$f->add_attr( array( 'cols'=>20, 'rows'=>6 ) ); 
 
// Another HTML only field (span) 
$f = new HTMLFormInput ( 'htmltext', 'sometext' ); 
$f->set_value( "This is a wonderful text!" ); 
$f->add_attr( array( 'style'=>'color:green;' ) );  
 
// This is not a database field, we just create it for fun. We won't map it to our block, 
// so it always initialize itself with the value given at creation time (or by ::init() or ::set_value()) 
// which is here the current time. 
$f = new HTMLFormDate( 'now', 'Actual time', time() ); 
$f->set_language( 'uk' ); 
 
// Our submit button. Even with no primary reference variable, you can immediately 
// access it after creation using FLIB::HTML('save').  
new HTMLFormControl( 'save', 'Update it!', 'submit' ); 
 
// A simple non functional tag.  
$h1 = new HTMLElement ( 'hr', HT_NOENDTAG, array ( 'noshade'=>true ) ); 
$h1->set_key( 'hr1' ); 
 
/* Main execution part */ 
$form->set_template( $template ); 
$form->add_blocks(); 
$form->add_fields(); 
 
// We map some defined fields to the database block, so the library knows how to update them 
// Note how the formslib field "memo" is mapped to the column "description" in the database block, 
// because of its different name. 
$form->map( 'myblock', array( 'id', 'name', 'age', 'memo'=>'description' ) ); 
$form->setup(); 
/* End of main execution part */ 
 
// This is a very simple method of displaying some errors that occured. 
// Normally, you will want make this in a more beautiful manner ;) 
if ( sizeof ($form->get_errors() ) ) { 
    print "Errors occured: "; 
    print_r( $form -> get_errors() ); 
} 
 
// This is some other stuff we want to do before outputting the code (it's user specific and not 
// directly related to the formslib!) 
post_actions(); 
 
// This is the output function for creating the HTML code 
print $form->out(); 
 
// Defining a field trigger which modifies a value just before inserting 
// or updating the field named "id" into the database. We want to try to insert 
// "null" on new id records (forcing mysql to use the auto increment function for this column). 
function _test_id_success ( $id ) { 
    return ( $id == 'new' ? null : $id ); 
} 
 
// This trigger is called automatically AFTER updating the data blocks. 
function _test_success() { 
    if ( $_GET['navigation'] != 1 ) { 
        FLIB::HTML('id')->populate( 'select id, name from person', false, $GLOBALS['oDB'] ); 
        if ( ! (int)$GLOBALS['id'] ) { 
            $GLOBALS['id'] = (int)$GLOBALS['oDB']->GetOne("select max(id) from person"); 
            FLIB::HTML('id')->set_value( $GLOBALS['id'] ); 
            post_actions(); 
        } 
    } 
} 
 
// An user specified validation function 
function my_validate_function ( $str ) 
{ 
    return ( $str{0} == strtoupper ( $str{0} ) ); 
} 
 
// We just pack this code into one function, because it is executed from 2 different locations 
function post_actions() 
{ 
    // Playing around with the button value depending on the record type ( new or update ) 
    // Have a look at the way we're accessing the field objects inside the form 
    if ( ! $GLOBALS['id'] || $GLOBALS['id'] == 'new' ) { 
        FLIB::HTML('save')->set_value( 'Insert it!' ); 
    } 
} 
 
?>
 
 |