Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 25th, 2012  |  syntax: None  |  size: 6.65 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. PEAR HTML_Quick Form Functions And Processing Data
  2. <html>
  3. <head></head>
  4. <body>
  5.      <?php
  6.  
  7.      require_once "HTML/QuickForm.php";
  8.     $form = new HTML_QuickForm('UserRegistration', 'POST');
  9.  
  10.       //Form elements***********************************************
  11.         form->addElement('header', 'RegistrationHeader', 'Fill in your details');
  12.     $form->addElement('text', 'firstname', 'First Name', array('size' => 49, 'maxlength'=>49));      
  13.     $form->addElement('text', 'lastname', 'Last Name', array('size' => 49, 'maxlength'=>49));
  14.     $form->addElement('text', 'username', 'Username',array('size'=> 49, 'maxlength'=>49));
  15.      $form->addElement('password', 'password', 'Password', array('size' => 30, 'maxlength'=>30));
  16.       $form->addElement('password', 'confirmpassword', 'Confirm Password', array('size'=> 30, 'maxlength'=>30));
  17.        $form->addElement('text', 'email', 'Email', array('size'=> 49, 'maxlength'=>49));
  18.         $form->addElement('text', 'confirmemail', 'Confirm Email', array('size'=>49, 'maxlength'=>49));
  19.         $form->addElement('hidden', 'ip', $_SERVER['REMOTE_ADDR']);
  20.       $buttons[] = &HTML_QuickForm::createElement('reset', 'null', 'Clear');
  21.       $buttons[] = &HTML_QuickForm::createElement('submit', 'null', 'Submit');
  22.       $form->addGroup($buttons, null, null, '&nbsp;');
  23.  
  24.       //***********************************************************************************************************
  25.  
  26.       //Setting of form functions*************************************************
  27.       //Email DNS check function
  28.       function checkEmailDNS($email, $domainCheck = false)
  29.     {
  30.     if (preg_match('/^[a-zA-Z0-9._-]+@([?)[a-zA-Z0-9-.]+'.
  31.                    '.([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$/', $email)) {
  32.         if ($domainCheck && function_exists('checkdnsrr')) {
  33.             list (, $domain)  = explode('@', $email);
  34.             if (checkdnsrr($domain, 'MX') || checkdnsrr($domain, 'A')) {
  35.                 return true;
  36.             }
  37.             return false;
  38.         }
  39.         return true;
  40.     }
  41.     return false;
  42.     }
  43.     $form->registerRule('checkmailDNS', 'callback', 'checkEmailDNS');
  44.        //******************************************************************************************
  45.        //Check for Email existance in the DataBase
  46.        function checkEmailfromDB($value)
  47.     {
  48.       try {
  49.  
  50.        $pdo = new PDO('mysql:dbname=mytestsite;host=localhost', 'root', '');
  51.  
  52.     } catch (PDOException $e) {
  53.  
  54.        die('ERROR: Cannot connect: ' . $e->getMessage());
  55.     }
  56.     $value = $pdo->quote($value);
  57.  
  58.     $sql = "SELECT userID FROM users WHERE userEMail= $value;";
  59.  
  60.     $ret = $pdo->query($sql) or die('ERROR: ' . implode(':', $pdo->errorInfo()));
  61.  
  62.     $str = $ret->fetchColumn();
  63.  
  64.     $flag = ($str == false) ? true : false;
  65.  
  66.     unset($pdo);
  67.  
  68.     return $flag;        
  69.       }
  70.        //*******************************************************************
  71.         //****************Check for username existance in the DataBase
  72.        function checkusernamefromDB($value)
  73.         {
  74.         try {
  75.  
  76.        $pdo = new PDO('mysql:dbname=mytestsite;host=localhost', 'root', '');
  77.  
  78.     } catch (PDOException $e) {
  79.  
  80.        die('ERROR: Cannot connect: ' . $e->getMessage());
  81.     }
  82.     $value = $pdo->quote($value);
  83.  
  84.     $sql = "SELECT userID FROM users WHERE userName= $value;";
  85.  
  86.     $ret = $pdo->query($sql) or die('ERROR: ' . implode(':', $pdo->errorInfo()));
  87.  
  88.     $str = $ret->fetchColumn();
  89.  
  90.     $flag = ($str == false) ? true : false;
  91.  
  92.     unset($pdo);
  93.  
  94.     return $flag;        
  95.       }
  96.         //**************************************************
  97.     //Form   Rules********************************************
  98.       $form->addRule('firstname', 'Your  First name is required', 'required');
  99.       $form->addRule('lastname', 'Your Last name is required', 'required');
  100.        $form->addRule('username', 'Username is required', 'required');                
  101.        $form->addRule('úsername', 'Username is already in use, choose a different one', 'callback', 'checkusernamefromDB');    
  102.          $form->addRule('password', 'Error: Enter a password', 'required');
  103.       $form->addRule('password', 'Error: The password should be at least 6 characters long', 'rangelength', array(6,30));
  104.        $form->addRule('confirmpassword', 'Error: Password confirmation is required', 'required');
  105.        $form->addRule('confirmpassword', 'Error: The password should be at least 6 characters long', 'rangelength', array(6,30));
  106.        $form->addRule(array('password','confirmpassword'), 'ERROR: Password mismatch', 'compare');
  107.       $form->addRule('email', 'Emal is required', 'required');
  108.       $form->addRule('email', 'Enter a valid Email adress', 'email');
  109.       $form->addRule('email', 'Email is incorrect', 'checkmailDNS', true);
  110.       $form->addRule('email','Email is already in use on the system', 'callback', 'checkEmailfromDB');
  111.       $form->addRule('confirmemail','Email confirmation is required','required');
  112.       $form->addRule('confirmemail', 'Email is incorrect', 'checkmailDNS', true);
  113.       $form->addRule('email','Email is already in use on the system', 'callback', 'checkEmailfromDB');
  114.       $form->addRule(array('email', 'confirmemail'), 'Error: Email mismatch', 'compare');
  115.       //***********************************************************************************************************
  116.  
  117.       //Form Filters*********************************************************************************************
  118.       $form->applyFilter('_ALL_', 'trim');
  119.        $form->applyFilter('firstname', 'lettersonly');
  120.        $form->applyFilter('firstname', 'strtolower');
  121.         $form->applyFilter('firstname', 'ucfirst');  
  122.          $form->applyFilter('lastname', 'lettersonly');
  123.          $form->applyFilter('lastname', 'strtolower');
  124.           $form->applyFilter('lastname', 'ucfirst');
  125.        $form->applyFilter('email', 'strtolower');                
  126.         $form->applyFilter('confirmemail', 'strtolower');
  127.  
  128.       //Display Form************************************************************
  129.        if ($form->validate()) {
  130.  
  131.           $form->freeze();
  132.       }
  133.       $form->display();  
  134.     ?>                                                                                                              
  135.  
  136. </body>
  137. </html>
  138.        
  139. if ($form->validate()) {
  140.    $user = new DataObjects_User;
  141.    $user->username = $form->exportValue('username');
  142.    if($user->find()) {
  143.       // username is already exists in the db
  144.    }
  145.  
  146. }
  147.        
  148. if ($form->validate()) {
  149.     $username = $form->exportValue('username');
  150.     $email = $form->exportValue('password');
  151.  
  152.     if( !checkusernamefromDB($username) or !checkEmailfromDB($email) ) {
  153.        $form->display();
  154.     } else {
  155.        $sql = "INSERT INTO users (userID,userEMail) VALUES($username, $email)";
  156.        $pdo->query($sql);
  157.     }
  158.  
  159. }