<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html" 
    charset="UTF-8">
  <title>Testing PEAR HTML_QuickForm</title>
</head>
<body>
<?php
  require_once "HTML/QuickForm.php";

  $form = new HTML_QuickForm('frmTest', 'get');
  $form->addElement('header', 'hdrQuickformtest', 
    'QuickForm Example 6');
  $form->addElement('text', 'txtName', 
    'What is your name?');

  $form->addElement('text', 'txtEmail', 'Email address:');
  $form->addElement('advcheckbox', 'chkNewsletter', 
    'Receive newsletter?');

  $form->addElement('reset', 'btnClear', 'Clear');
  $form->addElement('submit', 'btnSubmit', 'Submit');

  $form->applyFilter('__ALL__', 'trim');

  $form->addRule('txtName', 'Please enter your name', 
    'required');
  $form->addRule('txtEmail', 
    'Please enter a valid email address', 'email');
  $form->addFormRule('newcontact_validate');

  if ($form->validate()) {
    # If the form validates, freeze and process the data
    $form->freeze();
    $form->process("process_data", false);
  }
  else {
    $form->display();
  }

  function newcontact_validate ($fields) {

    # Set up array to store any errors
    $errors = array();

    # Carry out validation checks
    if ("1" == $fields['chkNewsletter']) {
      if (empty($fields['txtEmail'])) {
        $errors['txtEmail'] = 
          "Please enter an email address for the newsletter";
      }
    }

    # Return the errors if we have any, or TRUE if not
    if (empty($errors)) {
      return TRUE;
    }
    else {
      return $errors;
    }
  }

  function process_data ($values) {
    echo "<pre>";
    foreach ($values as $key=>$value) {
      echo $key."=".$value."<br>";
    }
    echo "</pre>";
  }
?>
</body>
</html>