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 - Register</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.         // add a label to the 'firstname' control - used in the template file as {controls.label_firstname}
  24.                 $form->add('label''label_firstname''firstname''First name:');
  25.  
  26.         // add a text control named "firstname" - used in the template file as {controls.firstname}
  27.                 $obj $form->add('text''firstname');
  28.         
  29.         // set the field as mandatory
  30.                 $obj->setRule(array('mandatory' => array('e1''The above field is required!')));
  31.  
  32.         // add a label to the 'lastname' control - used in the template file as {controls.label_lastname}
  33.                 $form->add('label''label_lastname''lastname''Last name:');
  34.  
  35.         // add a text control named "lastname" - used in the template file as {controls.lastname}
  36.                 $obj $form->add('text''lastname');
  37.  
  38.         // set the field as mandatory
  39.                 $obj->setRule(array('mandatory' => array('e2''The above field is required!')));
  40.  
  41.         // add a label to the 'email' control - used in the template file as {controls.label_email}
  42.                 $form->add('label''label_email''name''Email address:');
  43.  
  44.         // add a text control named "email" - used in the template file as {controls.email}
  45.                 $obj $form->add('text''email');
  46.  
  47.         // set the field as mandatory
  48.                 $obj->setRule(array('mandatory' => array('e3''The above field is required!')'email' => array('e2''Address seems to be invalid!')));
  49.  
  50.         // require a valid email address to be entered
  51.                 $obj->setRule(array('email' => array('e3''Address seems to be invalid!')));
  52.         
  53.         // add a label to the 'password' control - used in the template file as {controls.label_password}
  54.                 $form->add('label''label_password''password''Choose a password:');
  55.  
  56.         // add a text control named "password" - used in the template file as {controls.password}
  57.                 $obj $form->add('password''password');
  58.         
  59.         // set the field as mandatory
  60.                 $obj->setRule(array('mandatory' => array('e4''Password is required!')));
  61.  
  62.         // also, make the minimum allowed length for the password to be 6 characters
  63.                 $obj->setRule(array('minlength' => array('6''e4''The minimum length for passwords is 6 characters')));
  64.  
  65.         // add a label to the 'confirm_password' control - used in the template file as {controls.label_confirm_password}
  66.                 $form->add('label''label_confirm_password''confirm_password''Confirm password:');
  67.  
  68.         // add a text control named "confirm_password" - used in the template file as {controls.confirm_password}
  69.                 $obj $form->add('password''confirm_password');
  70.  
  71.         // set a rule for this control - it's value must be equal to the value entered in the 'password' field and
  72.         // NOTE THAT WE'RE SENDING THE MESSAGE TO TO e4 BLOCK WHERE THE ERROR'S GENERATED BY THE 'password' FIELD ARE ALSO SENT
  73.                 $obj->setRule(array('compare' => array('password''e4''Password not confirmed correctly!')));
  74.         
  75.         // generate a CAPTCHA image - used in the template file as {controls.captcha}
  76.                 $form->add('captcha''captcha');
  77.         
  78.         // add a label to the 'captcha_code' control - used in the template file as {controls.label_captcha_code}
  79.                 $form->add('label''label_captcha_code''captcha_code''Enter the characters you see in the picture:');
  80.  
  81.         // add a text control named "captcha_code" - used in the template file as {controls.captcha_code}
  82.                 $obj $form->add('text''captcha_code');
  83.         
  84.         // validate the entered text only if it's the same as the characters in the CAPTCHA image
  85.                 $obj->setRule(array('captcha' => array('e5''Enter the characters in the picture!')));
  86.  
  87.         $form->add('submit''submit''Submit');
  88.  
  89.         // validate the form
  90.                 if ($form->validate()) {
  91.         
  92.             // code if form is valid
  93.                         print_r('Form is valid. Do your thing (write to db, send emails, whatever) and redirect.');
  94.  
  95.             die();
  96.  
  97.         }
  98.         
  99.         // display the form with the specified template
  100.                 $form->render('forms/register.xtpl');
  101.  
  102.         // IF YOU DO NOT NEED ANY SPECIAL FORMATTINGS IN YOUR FORM YOU CAN ALSO CALL THE RENDER METHOD WITHOUT SPECIFYING A TEMPLATE NAME
  103.         // AND THUS LETTING THE SCRIPT TO AUTOMATICALLY GENERATE OUTPUT AND SAVING YOU OF EXTRA WORK PUT INTO DESIGNING THE FORM'S LAYOUT
  104.         // COMMENT THE LINE ABOVE AND UNCOMMENT THE ONE BELOW TO SEE IT IN ACTION
  105.  
  106.         //$form->render();
  107.         
  108.             
  109.     ?>
  110.  
  111.     </body>
  112.  
  113. </html>

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