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 - Radio controls, Checkboxes, and shorthand for custom error messages</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.     <?php
  16.     
  17.         // require the htmlform class
  18.                 require '../class.htmlform.php';
  19.  
  20.         // instantiate the class
  21.                 $form new HTMLForm('form''post');
  22.         
  23.         // we're adding a label whose id is label_options but we're not linking it to any control (the third argument is blank)
  24.                 $form->add('label''label_options''''Select an option:');
  25.  
  26.         // we're adding a radio control whose name is options and whose value is o1
  27.                 $form->add('radio''options''o1');
  28.         
  29.         // we're attaching a label to the previously added control
  30.         // notice how we're doing that: we're using the name of the control + underscore + the value of the control!
  31.                 $form->add('label''label_o1''options_o1''option 1');
  32.  
  33.         // we're adding a radio control whose name is options and whose value is o1
  34.                 $form->add('radio''options''o2');
  35.  
  36.         // we're attaching a label to the previously added control
  37.         // notice how we're doing that: we're using the name of the control + underscore + the value of the control!
  38.                 $form->add('label''label_o2''options_o2''option 2');
  39.         
  40.         // we're adding a radio control whose name is options and whose value is c1
  41.                 $form->add('checkbox''checkbox[]''c1');
  42.  
  43.         // we're attaching a label to the previously added control
  44.         // notice how we're doing that: we're using the name of the control (without the []) + underscore + the value of the control!
  45.                 $form->add('label''label_c1''checkbox_c1''option 1');
  46.  
  47.         // we're adding a radio control whose name is options and whose value is o1
  48.                 $form->add('checkbox''checkbox[]''c2');
  49.  
  50.         // we're attaching a label to the previously added control
  51.         // notice how we're doing that: we're using the name of the control (without the []) + underscore + the value of the control!
  52.                 $form->add('label''label_c2''checkbox_c2''option 2');
  53.  
  54.         // add the submit button
  55.                 $form->add('submit''submit''Submit');
  56.  
  57.         // validate the form
  58.                 if ($form->validate()) {
  59.         
  60.             // quick custom validation
  61.                         if (!isset($_POST['checkbox']|| !in_array('c2'$_POST['checkbox'])) {
  62.  
  63.                 // notice that we bind the error message to the last label - we do this so that we won't trigger an error when choosing
  64.                 // to let the script to automatically generate our form's output
  65.                                 $form->addError('e1''You MUST select checkbox option 2!''label_c2');
  66.  
  67.             else {
  68.         
  69.                 // code if form is valid
  70.                                 print_r('Form is valid. Do your thing (write to db, send emails, whatever) and redirect.');
  71.  
  72.                 die();
  73.                 
  74.             }
  75.             
  76.         }
  77.         
  78.         // display the form with the specified template
  79.                 $form->render('forms/radio.xtpl');
  80.  
  81.         // IF YOU DO NOT NEED ANY SPECIAL FORMATTINGS IN YOUR FORM YOU CAN ALSO CALL THE RENDER METHOD WITHOUT SPECIFYING A TEMPLATE NAME
  82.         // AND THUS LETTING THE SCRIPT TO AUTOMATICALLY GENERATE OUTPUT AND SAVING YOU OF EXTRA WORK PUT INTO DESIGNING THE FORM'S LAYOUT
  83.         // COMMENT THE LINE ABOVE AND UNCOMMENT THE ONE BELOW TO SEE IT IN ACTION
  84.  
  85.         //$form->render();
  86.  
  87.     
  88.     ?>
  89.     </body>
  90. </html>

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