<?php
 
/**
 
 * Create form in table and proccess it
 
 */
 
$start = utime();
 
 
include './Base.lib.php';
 
include './FormControls.lib.php';
 
include './FormControlSets.lib.php';
 
 
$Form = new TableForm($PHP_SELF);
 
 
$Form->setTableAttr(array('border'=>1, 'cellpadding'=>4, 'cellspacing'=>0));
 
$Form->setTableSize(2);
 
 
$Form->add(new CData('New User registration'), '', '', 2);
 
 
$Nick = new TextField('nick', '', 'Nick name');
 
$Nick->setReg('/^\w{3,20}$/i');
 
$Form->addFieldWithLabel($Nick);
 
 
$Email = new TextField('email', '[email protected]', 'Email');
 
$Email->setReg('/^[a-z0-9_\-.]{2,20}@[a-z0-9_\-.]{2,20}\.[a-z]{2,4}$/i');
 
$Form->addFieldWithLabel($Email);
 
 
$Birthday = new DateField('', 'birthday', '', 'Your birthday');
 
$Form->addFieldWithLabel($Birthday);
 
 
$Drink = new RadioSet('div', 'drink', 'tea', 'Drink, your prefer');
 
$Drink->addOption('tea');
 
$Drink->addOption('coffee');
 
$Drink->addOption('beer');
 
$Form->addFieldWithLabel($Drink);
 
 
$Os = new CheckBoxSet('div', 'os', '', 'System');
 
$Os->addOption(array('Linux', 'Solaris', 'Unix', 'Mac', 'Windows'));
 
$Os->setRange(1, 3);
 
$Os->ErrMsg = 'You should select from 1 to 3 os from list :)';
 
$Form->addFieldWithLabel($Os);
 
 
$Prog = new Select('prog', '', 'Programming language', array('multiple'=>'on', 'size'=>3));
 
$prog_lang = array(1=>'C++', 'Java', 'Pithon', 'PHP', 'Lisp', 'Ruby');
 
$Prog->addOption($prog_lang, '', true);
 
$Form->addFieldWithLabel($Prog);
 
 
$Form->add(new Submit(), '','',2);
 
 
 
/* print (and valid) form */
 
 
if (!$Form->isSubmit()) {
 
    /* show form first time */
 
    $Form->view();
 
 
} else {
 
    /* form was subitted */
 
    if (!$Form->valid()) {
 
        /* form filled incorect - show error mesage */
 
        echo "Some errors found while proccessing form<br>";
 
        echo $Form->getErrMsg();
 
        /* show form again */
 
        $Form->view();
 
    } else {
 
        /* get form values */
 
        $res = $Form->getValue();
 
        
 
        /* OK here our data */
 
        
 
        echo 'Nick: '.$res['nick'].'<br>';
 
        echo 'Email: '.$res['email'].'<br>';
 
        echo 'Birthday: '.$res['birthday'].'<br>';
 
        echo 'Drink Your prefer: '.$res['drink'].'<br>';
 
        echo 'Your OS: '.implode(', ', $res['os']).'<br>';
 
        if ($res['prog']) {
 
            echo 'Programming languages, Your know: ';
 
            
 
            foreach($res['prog'] as $id) {
 
                echo $prog_lang[$id] . ', ';
 
            }
 
            echo '<br>';
 
        }
 
    
 
    }
 
    
 
}
 
 
 
echo "<br>Page create in: ".(utime()-$start)." sec.<br>";
 
 
function utime ()
 
{
 
    $time = explode( " ", microtime());
 
    $usec = (double)$time[0];
 
    $sec = (double)$time[1];
 
    return $sec + $usec;
 
}
 
 
?>
 
 
 |