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 - Send To A Friend</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.     <p><strong>Quick tip: </strong>Don't worry that you don't have all the imaginable rules out of the box...Use the "custom" rule to create your owns!</p>
  16.  
  17.     <?php
  18.  
  19.         // require the htmlform class
  20.                 require '../class.htmlform.php';
  21.  
  22.         // instantiate the class
  23.                 $form new HTMLForm('form''post');
  24.         
  25.         // add a label to the 'name' control - used in the template file as {controls.label_name}
  26.                 $form->add('label''label_name''name''Your name:');
  27.  
  28.         // add a text control named "name" - used in the template file as {controls.name}
  29.                 $obj $form->add('text''name');
  30.         
  31.         // set the field as mandatory
  32.                 $obj->setRule(array('mandatory' => array('e1''The above field is required!')));
  33.  
  34.         // add a label to the 'email' control - used in the template file as {controls.label_email}
  35.                 $form->add('label''label_email''name''Your email address:');
  36.  
  37.         // add a text control named "email" - used in the template file as {controls.email}
  38.                 $obj $form->add('text''email');
  39.  
  40.         // set the field as mandatory
  41.                 $obj->setRule(array('mandatory' => array('e2''The above field is required!')'email' => array('e2''Invalid email address!')));
  42.  
  43.         // require a valid email address to be entered
  44.                 $obj->setRule(array('email' => array('e2''Address seems to be invalid!')));
  45.  
  46.         // add a label to the 'friends_name' control - used in the template file as {controls.label_friends_name}
  47.                 $form->add('label''label_friends_name''friends_name''Your friend\'s name:');
  48.  
  49.         // add a text control named "friends_name" - used in the template file as {controls.friends_name}
  50.                 $obj $form->add('text''friends_name');
  51.  
  52.         // set the field as mandatory
  53.                 $obj->setRule(array('mandatory' => array('e3''The above field is required!')));
  54.  
  55.         // add a label to the 'friends_email' control - used in the template file as {controls.label_friends_email}
  56.                 $form->add('label''label_friends_email''friends_email''Your friend\'s email address:');
  57.  
  58.         // add a text control named "friends_email" - used in the template file as {controls.friends_email}
  59.                 $obj $form->add('text''friends_email');
  60.  
  61.         // set the field as mandatory
  62.                 $obj->setRule(array('mandatory' => array('e4''The above field is required!')'email' => array('e4''Invalid email address!')));
  63.  
  64.         // require a valid email address to be entered
  65.                 $obj->setRule(array('email' => array('e4''Address seems to be invalid!')));
  66.  
  67.         // add a label to the 'message' control - used in the template file as {controls.label_message}
  68.                 $form->add('label''label_message''message''Message:');
  69.  
  70.         // add a text control named "message" - used in the template file as {controls.message}
  71.                 $obj $form->add('textarea''message'''array('style' => 'width:200px;height:100px'));
  72.  
  73.         // set the field as mandatory
  74.                 $obj->setRule(array('mandatory' => array('e5''The above field is required!')));
  75.         
  76.         $form->add('submit''submit''Submit');
  77.  
  78.         // validate the form
  79.                 if ($form->validate()) {
  80.         
  81.             // code if form is valid
  82.                         print_r('Form is valid. Do your thing (write to db, send emails, whatever) and redirect.');
  83.  
  84.             die();
  85.             
  86.         }
  87.         
  88.         // display the form with the specified template
  89.                 $form->render('forms/sendtoafriend.xtpl');
  90.  
  91.         // IF YOU DO NOT NEED ANY SPECIAL FORMATTINGS IN YOUR FORM YOU CAN ALSO CALL THE RENDER METHOD WITHOUT SPECIFYING A TEMPLATE NAME
  92.         // AND THUS LETTING THE SCRIPT TO AUTOMATICALLY GENERATE OUTPUT AND SAVING YOU OF EXTRA WORK PUT INTO DESIGNING THE FORM'S LAYOUT
  93.         // COMMENT THE LINE ABOVE AND UNCOMMENT THE ONE BELOW TO SEE IT IN ACTION
  94.  
  95.         //$form->render();
  96.  
  97.     
  98.     ?>
  99.  
  100.     </body>
  101.  
  102. </html>

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