Search This Blog

Thursday, July 5, 2012

Day 19 : Quickform2 tutorial

// Load the main class
require_once 'HTML/QuickForm2.php';

// Instantiate the HTML_QuickForm2 object
$form = new HTML_QuickForm2('tutorial');

// Set defaults for the form elements
$form->addDataSource(new HTML_QuickForm2_DataSource_Array(array(
'name' => 'Joe User',
'Gender' => '1',
'status' =>'1'
)));

// Add some elements to the form
$fieldset = $form->addElement('fieldset')->setLabel('QuickForm2 tutorial example');
$name = $fieldset->addElement('text', 'name', array('size' => 50, 'maxlength' => 255))
->setLabel('Your name:');
$email = $fieldset->addElement('text', 'email', array('size' => 50, 'maxlength' => 255))
->setLabel('Your Email:');

$gender1= $fieldset->addElement('radio', 'gender[]', array('value' => 1),
array('content' => 'male', 'label' => 'male'));
$gender2= $fieldset->addElement('radio', 'gender[]', array('value' => 2),
array('content' => 'female', 'label' => 'female'));

$options = array('KL','Melaka','Sabah', 'Srwk');
$location= $fieldset->addElement('select', 'location', array('multiple' => 'multiple', 'size' => 4),
array('options' => $options, 'label' => 'Multiple select Location:'));

$status1= $fieldset->addElement('checkbox', 'status[]', array('value' => 1),
array('content' => 'single'));
$status2= $fieldset->addElement('checkbox', 'status[]', array('value' => 2),
array('content' => 'married'));
$fieldset->addElement('submit', null, array('value' => 'Send!'));

// Define filters and validation rules
$name->addFilter('trim');
$name->addRule('required', 'Please enter your name');
$email->addFilter('trim');
$email->addRule('required', 'Please enter your email');
$email->addRule('regex', 'valid email address please','/^(\w+[\-\.])*\w+@(\w+\.)+[A-Za-z]+$/');

// Try to validate a form
if ($form->validate()) {
echo '<h1>Hello, ' . htmlspecialchars($name->getValue()) . '!</h1>';
//echo "<br/>".$email->getValue()."<br/>";
//var_dump($form->getValue());
//echo "<br/>".$form->getValue('name')."<br/>";
//echo "<br/>".$form['status']->getValue()."<br/>";
$post = $form->getValue();
//var_dump($post);
//var_dump($location->getValue();
var_dump($post['status']);
exit;
}

// Output the form
echo $form;

No comments:

Post a Comment