HTML_QuickForm2
[ class tree: HTML_QuickForm2 ] [ index: HTML_QuickForm2 ] [ all elements ]

Source for file basic-elements.php

Documentation is available at basic-elements.php

  1. <?php
  2. /**
  3.  * Usage example for HTML_QuickForm2 package: basic elements
  4.  */
  5. ?>
  6. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  7.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  8. <html xmlns="http://www.w3.org/1999/xhtml">
  9.   <head>
  10.     <style type="text/css">
  11. /* Set up custom font and form width */
  12. body {
  13.     margin-left: 10px;
  14.     font-family: Arial,sans-serif;
  15.     font-size: small;
  16. }
  17.  
  18. .quickform {
  19.     min-width: 500px;
  20.     max-width: 600px;
  21.     width: 560px;
  22. }
  23.  
  24. /* Use default styles included with the package */
  25.  
  26. <?php
  27. if ('@data_dir@' != '@' . 'data_dir@') {
  28.     $filename = '@data_dir@/HTML_QuickForm2/quickform.css';
  29. } else {
  30.     $filename = dirname(dirname(dirname(__FILE__))) . '/data/quickform.css';
  31. }
  32. readfile($filename);
  33. ?>
  34.     </style>
  35.     <title>HTML_QuickForm2 basic elements example</title>
  36.   </head>
  37.   <body>
  38. <?php
  39.  
  40. $options = array(
  41.     'a' => 'Letter A', 'b' => 'Letter B', 'c' => 'Letter C',
  42.     'd' => 'Letter D', 'e' => 'Letter E', 'f' => 'Letter F'
  43. );
  44.  
  45. $main = array("Pop", "Rock", "Classical");
  46.  
  47. $secondary = array(
  48.     array(0 => "Belle & Sebastian", 1 => "Elliot Smith", 2 => "Beck"),
  49.     array(3 => "Noir Desir", 4 => "Violent Femmes"),
  50.     array(5 => "Wagner", 6 => "Mozart", 7 => "Beethoven")
  51. );
  52.  
  53. require_once 'HTML/QuickForm2.php';
  54. require_once 'HTML/QuickForm2/Renderer.php';
  55.  
  56. $form = new HTML_QuickForm2('elements');
  57.  
  58. // data source with default values:
  59. $form->addDataSource(new HTML_QuickForm2_DataSource_Array(array(
  60.     'textTest'        => 'Some text',
  61.     'areaTest'        => "Some text\non multiple lines",
  62.     'userTest'        => 'luser',
  63.     'selSingleTest'   => 'f',
  64.     'selMultipleTest' => array('b', 'c'),
  65.     'boxTest'         => '1',
  66.     'radioTest'       => '2',
  67.     'testDate'        => time(),
  68.     'testHierselect'  => array(2, 5)
  69. )));
  70.  
  71. // text input elements
  72. $fsText = $form->addElement('fieldset')->setLabel('Text boxes');
  73. $fsText->addElement(
  74.     'text', 'textTest', array('style' => 'width: 300px;'), array('label' => 'Test Text:')
  75. );
  76. $fsText->addElement(
  77.     'password', 'pwdTest', array('style' => 'width: 300px;'), array('label' => 'Test Password:')
  78. );
  79. $area = $fsText->addElement(
  80.     'textarea', 'areaTest', array('style' => 'width: 300px;', 'cols' => 50, 'rows' => 7),
  81.     array('label' => 'Test Textarea:')
  82. );
  83.  
  84. $fsNested = $form->addElement('fieldset')->setLabel('Nested fieldset');
  85. $fsNested->addElement(
  86.     'text', 'userTest', array('style' => 'width: 200px'), array('label' => 'Username:')
  87. );
  88. $fsNested->addElement(
  89.     'password', 'passTest', array('style' => 'width: 200px'), array('label' => 'Password:')
  90. );
  91. // Now we move the fieldset into another fieldset!
  92. $fsText->insertBefore($fsNested, $area);
  93.  
  94.  
  95. // selects
  96. $fsSelect = $form->addElement('fieldset')->setLabel('Selects');
  97. $fsSelect->addElement(
  98.     'select', 'selSingleTest', null, array('options' => $options, 'label' => 'Single select:')
  99. );
  100. $fsSelect->addElement(
  101.     'select', 'selMultipleTest', array('multiple' => 'multiple', 'size' => 4),
  102.     array('options' => $options, 'label' => 'Multiple select:')
  103. );
  104.  
  105. // checkboxes and radios
  106. $fsCheck = $form->addElement('fieldset')->setLabel('Checkboxes and radios');
  107. $fsCheck->addElement(
  108.     'checkbox', 'boxTest', null, array('content' => 'check me', 'label' => 'Test Checkbox:')
  109. );
  110. $fsCheck->addElement(
  111.     'radio', 'radioTest', array('value' => 1), array('content' => 'select radio #1', 'label' => 'Test radio:')
  112. );
  113. $fsCheck->addElement(
  114.     'radio', 'radioTest', array('value' => 2), array('content' => 'select radio #2', 'label' => '(continued)')
  115. );
  116.  
  117. $fsCustom = $form->addElement('fieldset')->setLabel('Custom elements');
  118. $fsCustom->addElement(
  119.     'date', 'testDate', null,
  120.     array('format' => 'd-F-Y', 'minYear' => date('Y'), 'maxYear' => 2001)
  121. )->setLabel('Today is:');
  122.  
  123. $fsCustom->addElement('hierselect', 'testHierselect', array('style' => 'width: 20em;'))
  124.          ->setLabel('Hierarchical select:')
  125.          ->loadOptions(array($main, $secondary))
  126.          ->setSeparator('<br />');
  127.  
  128. // buttons
  129. $fsButton = $form->addElement('fieldset')->setLabel('Buttons');
  130. $testReset = $fsButton->addElement(
  131.     'reset', 'testReset', array('value' => 'This is a reset button')
  132. );
  133. $fsButton->addElement(
  134.     'inputbutton', 'testInputButton',
  135.     array('value' => 'Click this button', 'onclick' => "alert('This is a test.');")
  136. );
  137. $fsButton->addElement(
  138.     'button', 'testButton', array('onclick' => "alert('Almost nothing');", 'type' => 'button'),
  139.     array('content' => '<img src="http://pear.php.net/gifs/pear-icon.gif" '.
  140.         'width="32" height="32" alt="pear" />This button does almost nothing')
  141. );
  142. // submit buttons in nested fieldset
  143. $fsSubmit = $fsButton->addElement('fieldset')->setLabel('These buttons can submit the form');
  144. $fsSubmit->addElement(
  145.     'submit', 'testSubmit', array('value' => 'Test Submit')
  146. );
  147. $fsSubmit->addElement(
  148.     'button', 'testSubmitButton', array('type' => 'submit'),
  149.      array('content' => '<img src="http://pear.php.net/gifs/pear-icon.gif" '.
  150.         'width="32" height="32" alt="pear" />This button submits')
  151. );
  152. $fsSubmit->addElement(
  153.     'image', 'testImage', array('src' => 'http://pear.php.net/gifs/pear-icon.gif')
  154. );
  155.  
  156. // outputting form values
  157. if ('POST' == $_SERVER['REQUEST_METHOD']) {
  158.     echo "<pre>\n";
  159.     var_dump($form->getValue());
  160.     echo "</pre>\n<hr />";
  161.     // let's freeze the form and remove the reset button
  162.     $fsButton->removeChild($testReset);
  163.     $form->toggleFrozen(true);
  164. }
  165.  
  166. $renderer = HTML_QuickForm2_Renderer::factory('default');
  167. $form->render($renderer);
  168. // Output javascript libraries, needed by hierselect
  169. echo $renderer->getJavascriptBuilder()->getLibraries(true, true);
  170. echo $renderer;
  171. ?>
  172.   </body>
  173. </html>

Documentation generated on Wed, 10 Apr 2019 08:56:07 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.