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.5 2007/06/30 17:39:41 avb 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('fieldset')->setLabel('Text boxes');
  126. $fsText->addElement(
  127.     'text''textTest'array('style' => 'width: 300px;')array('label' => 'Test Text:'
  128. );
  129. $fsText->addElement(
  130.     'password''pwdTest'array('style' => 'width: 300px;')array('label' => 'Test Password:'
  131. );
  132. $area $fsText->addElement(
  133.     'textarea''areaTest'array('style' => 'width: 300px;''cols' => 50'rows' => 7),
  134.     array('label' => 'Test Textarea:')
  135. );
  136.  
  137. $fsNested $form->addElement('fieldset')->setLabel('Nested fieldset');
  138. $fsNested->addElement(
  139.     'text''userTest'array('style' => 'width: 200px')array('label' => 'Username:')
  140. );
  141. $fsNested->addElement(
  142.     'password''passTest'array('style' => 'width: 200px')array('label' => 'Password:')
  143. );
  144. // Now we move the fieldset into another fieldset!
  145. $fsText->insertBefore($fsNested$area);
  146.  
  147.  
  148. // selects
  149. $fsSelect $form->addElement('fieldset')->setLabel('Selects');
  150. $fsSelect->addElement(
  151.     'select''selSingleTest'nullarray('options' => $options'label' => 'Single select:')
  152. );
  153. $fsSelect->addElement(
  154.     'select''selMultipleTest'array('multiple' => 'multiple''size' => 4),
  155.     array('options' => $options'label' => 'Multiple select:')
  156. );
  157.  
  158. // checkboxes and radios
  159. $fsCheck $form->addElement('fieldset')->setLabel('Checkboxes and radios');
  160. $fsCheck->addElement(
  161.     'checkbox''boxTest'nullarray('content' => 'check me''label' => 'Test Checkbox:')
  162. );
  163. $fsCheck->addElement(
  164.     'radio''radioTest'array('value' => 1)array('content' => 'select radio #1''label' => 'Test radio:')
  165. );
  166. $fsCheck->addElement(
  167.     'radio''radioTest'array('value' => 2)array('content' => 'select radio #2''label' => '(continued)')
  168. );
  169.  
  170. // buttons
  171. $fsButton $form->addElement('fieldset')->setLabel('Buttons');
  172. $testReset $fsButton->addElement(
  173.     'reset''testReset'array('value' => 'This is a reset button')
  174. );
  175. $fsButton->addElement(
  176.     'inputbutton''testInputButton',
  177.     array('value' => 'Click this button''onclick' => "alert('This is a test.');")
  178. );
  179. $fsButton->addElement(
  180.     'button''testButton'array('onclick' => "alert('Almost nothing');"'type' => 'button')
  181.     array('content' => '<img src="http://pear.php.net/gifs/pear-icon.gif" '.
  182.         'width="32" height="32" alt="pear" />This button does almost nothing')
  183. );
  184. // submit buttons in nested fieldset
  185. $fsSubmit $fsButton->addElement('fieldset')->setLabel('These buttons can submit the form');
  186. $fsSubmit->addElement(
  187.     'submit''testSubmit'array('value' => 'Test Submit')
  188. );
  189. $fsSubmit->addElement(
  190.     'button''testSubmitButton'array('type' => 'submit')
  191.      array('content' => '<img src="http://pear.php.net/gifs/pear-icon.gif" '.
  192.         'width="32" height="32" alt="pear" />This button submits')
  193. );
  194. $fsSubmit->addElement(
  195.     'image''testImage'array('src' => 'http://pear.php.net/gifs/pear-icon.gif')
  196. );
  197.  
  198. // outputting form values
  199. if ('POST' == $_SERVER['REQUEST_METHOD']{
  200.     echo "<pre>\n";
  201.     var_dump($form->getValue());
  202.     echo "</pre>\n<hr />";
  203.     // let's freeze the form and remove the reset button
  204.     $fsButton->removeChild($testReset);
  205.     $form->toggleFrozen(true);
  206. }
  207.  
  208. echo '<form' $form->getAttributes(true">\n";
  209. foreach ($form as $element{
  210.     output_element($element);
  211. }
  212. ?>
  213. </form>
  214.   </body>
  215. </html>

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