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.  * $Id: basic-elements.php,v 1.2 2007/04/17 14:00:40 mansion Exp $
  6.  */ 
  7. ?>
  8. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  9.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  10. <html>
  11.   <head>
  12.     <style type="text/css">
  13. /* styles borrowed from an older release of Tableless Renderer for QF. 
  14.    Newer styles work worse with nested fieldsets */
  15. body {
  16.     margin-left: 10px;
  17.     font-family: Arial,sans-serif;
  18.     font-size: small;
  19. }
  20. form {
  21.     margin: 0;
  22.     padding: 0;
  23.     min-width: 500px;
  24.     max-width: 600px;
  25.     width: 560px;
  26. }
  27. form fieldset {
  28.     border: 1px solid black;
  29.     padding: 10px 5px;
  30.     margin: 0;
  31.     /*width: 560px;*/
  32. }
  33. form fieldset.hidden {
  34.     border: 0;
  35. }
  36. form fieldset legend {
  37.     font-weight: bold;
  38. }
  39. form label {
  40.     margin: 0 0 0 5px;
  41. }
  42. form label.qflabel {
  43.     display: block;
  44.     float: left;
  45.     width: 150px;
  46.     padding: 0;
  47.     margin: 5px 0 0 0;
  48.     text-align: right;
  49. }
  50. form input, form textarea, form select {
  51.     width: auto;
  52. }
  53. form textarea {
  54.     overflow: auto;
  55. }
  56. form br {
  57.     clear: left;
  58. }
  59. form div.qfelement {
  60.     display: inline;
  61.     float: left;
  62.     margin: 5px 0 0 10px;
  63.     padding: 0;
  64. }
  65. form div.qfreqnote {
  66.     font-size: 80%; 
  67. }
  68. form span.error, form span.required {
  69.     color: red;
  70. }
  71. form div.error {
  72.     border: 1px solid red;
  73.     padding: 5px;
  74. }
  75.     </style>
  76.     <title>HTML_QuickForm2 basic elements example</title>
  77.   </head>
  78.   <body>
  79. <?php
  80.  
  81. $options = array(
  82.     'a' => 'Letter A''b' => 'Letter B''c' => 'Letter C',
  83.     'd' => 'Letter D''e' => 'Letter E''f' => 'Letter F'
  84. );
  85.  
  86. function output_element($element)
  87. {
  88.     if ('fieldset' == $element->getType()) {
  89.         output_fieldset($element);
  90.     elseif ('hidden' == $element->getType()) {
  91.         echo '<div style="display: none;">' $element->__toString("</div>\n";
  92.     else {
  93.         echo '<div class="qfrow"><label class="qflabel" for="' $element->getId(.
  94.              '">' $element->getLabel('</label> <div class="qfelement">' .
  95.              $element->__toString("</div></div><br />\n";
  96.     }
  97. }
  98.  
  99. function output_fieldset($fieldset)
  100. {
  101.     echo '<fieldset' $fieldset->getAttributes(true">\n<legend>" .
  102.          $fieldset->getLabel("</legend>\n";
  103.     foreach ($fieldset as $element{
  104.         output_element($element);
  105.     }
  106.     echo "</fieldset>\n";
  107. }
  108.  
  109. require_once 'HTML/QuickForm2.php';
  110.  
  111. $form = new HTML_QuickForm2('elements');
  112.  
  113. // data source with default values:
  114. $form->addDataSource(new HTML_QuickForm2_DataSource_Array(array(
  115.     'textTest'        => 'Some text',
  116.     'areaTest'        => "Some text\non multiple lines",
  117.     'userTest'        => 'luser',
  118.     'selSingleTest'   => 'f',
  119.     'selMultipleTest' => array('b''c'),
  120.     'boxTest'         => '1',
  121.     'radioTest'       => '2'
  122. )));
  123.  
  124. // text input elements
  125. $fsText $form->addElement(
  126.     'fieldset'nullnull'Text boxes'
  127. );
  128. $fsText->addElement(
  129.     'text''textTest'null'Test Text:'array('style' => 'width: 300px;')
  130. );
  131. $fsText->addElement(
  132.     'password''pwdTest'null'Test Password:'array('style' => 'width: 300px;')
  133. );
  134. $area $fsText->addElement(
  135.     'textarea''areaTest'null'Test Textarea:',
  136.     array('style' => 'width: 300px;''cols' => 50'rows' => 7)
  137. );
  138.  
  139. $fsNested $form->addElement(
  140.     'fieldset'nullnull'Nested fieldset'
  141. );
  142. $fsNested->addElement(
  143.     'text''userTest'null'Username:'array('style' => 'width: 200px')
  144. );
  145. $fsNested->addElement(
  146.     'password''passTest'null'Password:'array('style' => 'width: 200px')
  147. );
  148. // Now we move the fieldset into another fieldset!
  149. $fsText->insertBefore($fsNested$area);
  150.  
  151.  
  152. // selects
  153. $fsSelect $form->addElement(
  154.     'fieldset'nullnull'Selects'
  155. );
  156. $fsSelect->addElement(
  157.     'select''selSingleTest'array('options' => $options)'Single select:'
  158. );
  159. $fsSelect->addElement(
  160.     'select''selMultipleTest'array('options' => $options)'Multiple select:',
  161.     array('multiple' => 'multiple''size' => 4)
  162. );
  163.  
  164. // checkboxes and radios
  165. $fsCheck $form->addElement(
  166.     'fieldset'nullnull'Checkboxes and radios'
  167. );
  168. $fsCheck->addElement(
  169.     'checkbox''boxTest'array('content' => 'check me')'Test Checkbox:'
  170. );
  171. $fsCheck->addElement(
  172.     'radio''radioTest'array('content' => 'select radio #1')'Test radio:'array('value' => 1)
  173. );
  174. $fsCheck->addElement(
  175.     'radio''radioTest'array('content' => 'select radio #2')'(continued)'array('value' => 2)
  176. );
  177.  
  178. // buttons
  179. $fsButton $form->addElement(
  180.     'fieldset'nullnull'Buttons'
  181. );
  182. $testReset $fsButton->addElement(
  183.     'reset''testReset'nullnullarray('value' => 'This is a reset button')
  184. );
  185. $fsButton->addElement(
  186.     'inputbutton''testInputButton'nullnull,
  187.     array('value' => 'Click this button''onclick' => "alert('This is a test.');")
  188. );
  189. $fsButton->addElement(
  190.     'button''testButton'array('content' => '<img src="http://pear.php.net/gifs/pear-icon.gif" '.
  191.         'width="32" height="32" alt="pear" />This button does almost nothing'),
  192.     nullarray('onclick' => "alert('Almost nothing');"'type' => 'button')
  193. );
  194. // submit buttons in nested fieldset
  195. $fsSubmit $fsButton->addElement(
  196.     'fieldset'nullnull'These buttons can submit the form'
  197. );
  198. $fsSubmit->addElement(
  199.     'submit''testSubmit'nullnullarray('value' => 'Test Submit')
  200. );
  201. $fsSubmit->addElement(
  202.     'button''testSubmitButton'array('content' => '<img src="http://pear.php.net/gifs/pear-icon.gif" '.
  203.         'width="32" height="32" alt="pear" />This button submits'),
  204.     nullarray('type' => 'submit')
  205. );
  206. $fsSubmit->addElement(
  207.     'image''testImage'nullnullarray('src' => 'http://pear.php.net/gifs/pear-icon.gif')
  208. );
  209.  
  210. // outputting form values
  211. if ('POST' == $_SERVER['REQUEST_METHOD']{
  212.     echo "<pre>\n";
  213.     var_dump($form->getValue());
  214.     echo "</pre>\n<hr />";
  215.     // let's freeze the form and remove the reset button
  216.     $fsButton->removeChild($testReset);
  217.     $form->toggleFrozen(true);
  218. }
  219.  
  220. echo '<form' $form->getAttributes(true">\n";
  221. foreach ($form as $element{
  222.     output_element($element);
  223. }
  224. ?>
  225. </form>
  226.   </body>
  227. </html>

Documentation generated on Mon, 11 Mar 2019 15:00:13 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.