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

example

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2.  
  3. <html>
  4.  
  5.     <head>
  6.  
  7.         <title>PHP HTML Form Processor Class - Other Controls</title>
  8.         
  9.         <style type="text/css"> body {font-family: tahoma, arial, verdana, sans; font-size: 12px; } </style>
  10.  
  11.     </head>
  12.  
  13.     <body>
  14.  
  15.     <h2>For this example to work you need the PHP Date Picker Class!</h2>
  16.  
  17.     <p><strong>Quick tip: </strong>Notice the '&'s in code - this is there in order for $obj to really point to the newly added control in PHP4</p>
  18.  
  19.     <?php
  20.  
  21.         // create a custom function that compares a control's
  22.         // submitted value with another value
  23.                 function valueCompare($controlValue$valueToCompareTo)
  24.         {
  25.  
  26.             // if values are not the same
  27.             if ($controlValue != $valueToCompareTo{
  28.  
  29.                 // return false
  30.                 return false;
  31.  
  32.             }
  33.  
  34.             // return true if everything is ok
  35.             return true;
  36.  
  37.         }
  38.  
  39.         // require the htmlform class
  40.                 require '../class.htmlform.php';
  41.  
  42.         // instantiate the class
  43.                 $form new HTMLForm('form''post');
  44.         
  45.         // specify the date to the datepicker class (a member of the Zebra PHP Framework)
  46.         // if class is not found the execution will break when you try to instantiate a date control
  47.                 $form->datePickerPath '../../datepicker/class.datepicker.php';
  48.  
  49.         // add a label to the 'admin' control - used in the template file as {controls.label_admin}
  50.                 $form->add('label''label_admin''name''Enter \'admin\' here');
  51.  
  52.         // add a text control named "admin" - used in the template file as {controls.admin}
  53.                 $obj $form->add('text''admin');
  54.  
  55.         // set the field as mandatory
  56.                 $obj->setRule(array('mandatory' => array('e1''The above field is mandatory!')));
  57.         
  58.         // add a custom validation rule
  59.                 $obj->setRule(array('custom' => array('valueCompare''admin''e1''You must enter \'admin\'!')));
  60.  
  61.         // add a label to the 'select' control - used in the template file as {controls.label_select}
  62.                 $form->add('label''label_select''select''Select a country');
  63.  
  64.         // add a select control named "select" - used in the template file as {controls.select}
  65.                 $obj $form->add('select''select');
  66.  
  67.         // add some options to the select control
  68.         // NOTE THAT, IF THE "MULTIPLE" ATTRIBUTE IS NOT SET, THE FIRST OPTION WILL BE ALWAYS CONSIDERED AS THE
  69.         // "NOTHING IS SELECTED" STATE OF THE CONTROL!
  70.                 $obj->addOptions(array('- select -''ro' => 'Romania''us' => 'United States''br' => 'Brazil''de' => 'Germany'));
  71.  
  72.         // set the field as mandatory
  73.                 $obj->setRule(array('mandatory' => array('e2''Select a country!')));
  74.  
  75.         // add a label to the 'option' control - used in the template file as {controls.label_option}
  76.                 $form->add('label''label_option''option''Select an option');
  77.  
  78.         // add a select control, having the name attribute set to "option[]" and the id attributes set to "option",
  79.         // no default value, and some HTML attributes set like "multiple", "size" and "style"
  80.         // note the '[]' at the end of the name in order to be able to submit selected options as an array
  81.         // also note that the id attribute will be name of the control with the '[]' stripped and therefore
  82.         // in the template file, you'll be able to call this control's output by using {controls.option}
  83.                 $obj $form->add('select''option[]'''array('multiple' => 'multiple''size' => 3'style' => 'height:60px'));
  84.  
  85.         // add some options to the control
  86.                 $obj->addOptions(array('I like this PHP class''I don\'t like this PHP class''What\'s PHP?'));
  87.  
  88.         // set the field as mandatory
  89.                 $obj->setRule(array('mandatory' => array('e3''Select at least one option!')));
  90.  
  91.         // add a label to the 'date' control - used in the template file as {controls.label_date}
  92.                 $form->add('label''label_date''date''What date is today?');
  93.  
  94.         // add a select control named "date" - used in the template file as {controls.date}
  95.                 $obj $form->add('date''date');
  96.  
  97.         // set some properties of the date picker
  98.                 $obj->datePicker->preselectedDate mktime(000date('m')date('d')date('Y'));
  99.  
  100.         $obj->datePicker->dateFormat 'd M Y';
  101.  
  102.         // set the field as mandatory
  103.                 $obj->setRule(array('mandatory' => array('e4''Please select a date!')));
  104.  
  105.         $obj $form->add('file''file');
  106.  
  107.         $obj->setRule(array('mandatory' => array('e5''Pick a file!')));
  108.  
  109.         // place a submit button
  110.                 $form->add('submit''submit''Submit');
  111.         
  112.         // validate the form
  113.                 if ($form->validate()) {
  114.         
  115.             // code if form is valid
  116.                         print_r('<p>Form is valid. Do your thing (write to db, send emails, whatever) and redirect.</p>');
  117.             
  118.             die();
  119.  
  120.         }
  121.         
  122.         // display the form with the specified template
  123.                 $form->render('forms/othercontrols.xtpl');
  124.         
  125.         // IF YOU DO NOT NEED ANY SPECIAL FORMATTINGS IN YOUR FORM YOU CAN ALSO CALL THE RENDER METHOD WITHOUT SPECIFYING A TEMPLATE NAME
  126.         // AND THUS LETTING THE SCRIPT TO AUTOMATICALLY GENERATE OUTPUT AND SAVING YOU OF EXTRA WORK PUT INTO DESIGNING THE FORM'S LAYOUT
  127.         // COMMENT THE LINE ABOVE AND UNCOMMENT THE ONE BELOW TO SEE IT IN ACTION
  128.  
  129.         //$form->render();
  130.  
  131.     
  132.     ?>
  133.     </body>
  134. </html>

Documentation generated on Mon, 22 Sep 2008 11:03:11 +0300 by phpDocumentor 1.3.0RC6