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

Source for file numeral-captcha.php

Documentation is available at numeral-captcha.php

  1. <?php session_start()?>
  2.  
  3. <?xml version="1.0" encoding="utf-8"?>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  5.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  6. <html>
  7.     <head>
  8.         <title>HTML_QuickForm2_Element_Captcha_Numeral demo</title>
  9.         <style type="text/css">
  10.             span.error {
  11.                 color: red;
  12.             }
  13.             div.row {
  14.                 clear: both;
  15.             }
  16.             label {
  17.                 width: 200px;
  18.                 float: left;
  19.             }
  20.             div.element {
  21.                 float: left;
  22.             }
  23.             div.element.error input {
  24.                 background-color: #FAA;
  25.             }
  26.         </style>
  27.     </head>
  28.     <body>
  29.  
  30. <?php
  31.  
  32. // Ignore E_STRICT errors coming from some modules
  33. error_reporting(E_ALL ~E_STRICT);
  34.  
  35. // You may not need to do this. For development purposes only
  36.     '../../'
  37.     . ':' get_include_path()
  38. );
  39.  
  40. // Include main quickform class
  41. require_once 'HTML/QuickForm2.php';
  42.  
  43. // Include the numeral captcha class file. necessary because
  44. // the QuickForm2 Captcha is separate from QuickForm2 itself.
  45. require_once 'HTML/QuickForm2/Element/Captcha/Numeral.php';
  46.  
  47. // Register the numeral captcha element with QuickForm2
  48. HTML_QuickForm2_Factory::registerElement(
  49.     'numeralcaptcha',
  50.     'HTML_QuickForm2_Element_Captcha_Numeral'
  51. );
  52.  
  53. // Create a new form
  54. $form = new HTML_QuickForm2(
  55.     'register''post'
  56. );
  57.  
  58. // Add some normal elements
  59.  
  60. // Username
  61. $username $form->addElement('text''username')
  62.     ->setLabel('Your username');
  63. $username->addRule('required''Username is required');
  64. $username->addRule('minlength''Username is too short'3);
  65.  
  66. // Password
  67. $password $form->addElement('password''password')
  68.     ->setLabel('Your password');
  69. $password->addRule('required''Password is required');
  70.  
  71. // Add the captcha element. no need to add rules!
  72. $captcha $form->addElement(
  73.     'numeralcaptcha',
  74.     'captchaelem',
  75.     array(
  76.         'id' => 'captchavalue',
  77.     ),
  78.  
  79.     // Set some captcha specific options
  80.     array(
  81.         'minValue' => 100,
  82.         'maxValue' => 200,
  83.     )
  84. )->setLabel('Anti-Spam question');
  85.  
  86. // Submit button
  87. $form->addElement(
  88.     'submit''submitted',
  89.     array(
  90.         'id'    => 'submit',
  91.         'value' => 'Try it',
  92.     )
  93. );
  94.  
  95. if ($form->validate()) {
  96.     echo '<h3>Form data valid</h3>';
  97.     echo 'In a real form, we would register the user now with the following data:';
  98.     echo '<pre>';
  99.     var_dump($form->getValue());
  100.     echo '</pre>';
  101.  
  102.     // Clear the session, otherwise the user can re-submit the form
  103.     // again and again without solving the captcha again
  104.     $captcha->clearCaptchaSession();
  105. else {
  106.     echo $form;
  107. }
  108.  
  109. ?>
  110.  
  111.     </body>
  112. </html>

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