- PEAR HTML_Quick Form Functions And Processing Data
- <html>
- <head></head>
- <body>
- <?php
- require_once "HTML/QuickForm.php";
- $form = new HTML_QuickForm('UserRegistration', 'POST');
- //Form elements***********************************************
- form->addElement('header', 'RegistrationHeader', 'Fill in your details');
- $form->addElement('text', 'firstname', 'First Name', array('size' => 49, 'maxlength'=>49));
- $form->addElement('text', 'lastname', 'Last Name', array('size' => 49, 'maxlength'=>49));
- $form->addElement('text', 'username', 'Username',array('size'=> 49, 'maxlength'=>49));
- $form->addElement('password', 'password', 'Password', array('size' => 30, 'maxlength'=>30));
- $form->addElement('password', 'confirmpassword', 'Confirm Password', array('size'=> 30, 'maxlength'=>30));
- $form->addElement('text', 'email', 'Email', array('size'=> 49, 'maxlength'=>49));
- $form->addElement('text', 'confirmemail', 'Confirm Email', array('size'=>49, 'maxlength'=>49));
- $form->addElement('hidden', 'ip', $_SERVER['REMOTE_ADDR']);
- $buttons[] = &HTML_QuickForm::createElement('reset', 'null', 'Clear');
- $buttons[] = &HTML_QuickForm::createElement('submit', 'null', 'Submit');
- $form->addGroup($buttons, null, null, ' ');
- //***********************************************************************************************************
- //Setting of form functions*************************************************
- //Email DNS check function
- function checkEmailDNS($email, $domainCheck = false)
- {
- if (preg_match('/^[a-zA-Z0-9._-]+@([?)[a-zA-Z0-9-.]+'.
- '.([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$/', $email)) {
- if ($domainCheck && function_exists('checkdnsrr')) {
- list (, $domain) = explode('@', $email);
- if (checkdnsrr($domain, 'MX') || checkdnsrr($domain, 'A')) {
- return true;
- }
- return false;
- }
- return true;
- }
- return false;
- }
- $form->registerRule('checkmailDNS', 'callback', 'checkEmailDNS');
- //******************************************************************************************
- //Check for Email existance in the DataBase
- function checkEmailfromDB($value)
- {
- try {
- $pdo = new PDO('mysql:dbname=mytestsite;host=localhost', 'root', '');
- } catch (PDOException $e) {
- die('ERROR: Cannot connect: ' . $e->getMessage());
- }
- $value = $pdo->quote($value);
- $sql = "SELECT userID FROM users WHERE userEMail= $value;";
- $ret = $pdo->query($sql) or die('ERROR: ' . implode(':', $pdo->errorInfo()));
- $str = $ret->fetchColumn();
- $flag = ($str == false) ? true : false;
- unset($pdo);
- return $flag;
- }
- //*******************************************************************
- //****************Check for username existance in the DataBase
- function checkusernamefromDB($value)
- {
- try {
- $pdo = new PDO('mysql:dbname=mytestsite;host=localhost', 'root', '');
- } catch (PDOException $e) {
- die('ERROR: Cannot connect: ' . $e->getMessage());
- }
- $value = $pdo->quote($value);
- $sql = "SELECT userID FROM users WHERE userName= $value;";
- $ret = $pdo->query($sql) or die('ERROR: ' . implode(':', $pdo->errorInfo()));
- $str = $ret->fetchColumn();
- $flag = ($str == false) ? true : false;
- unset($pdo);
- return $flag;
- }
- //**************************************************
- //Form Rules********************************************
- $form->addRule('firstname', 'Your First name is required', 'required');
- $form->addRule('lastname', 'Your Last name is required', 'required');
- $form->addRule('username', 'Username is required', 'required');
- $form->addRule('úsername', 'Username is already in use, choose a different one', 'callback', 'checkusernamefromDB');
- $form->addRule('password', 'Error: Enter a password', 'required');
- $form->addRule('password', 'Error: The password should be at least 6 characters long', 'rangelength', array(6,30));
- $form->addRule('confirmpassword', 'Error: Password confirmation is required', 'required');
- $form->addRule('confirmpassword', 'Error: The password should be at least 6 characters long', 'rangelength', array(6,30));
- $form->addRule(array('password','confirmpassword'), 'ERROR: Password mismatch', 'compare');
- $form->addRule('email', 'Emal is required', 'required');
- $form->addRule('email', 'Enter a valid Email adress', 'email');
- $form->addRule('email', 'Email is incorrect', 'checkmailDNS', true);
- $form->addRule('email','Email is already in use on the system', 'callback', 'checkEmailfromDB');
- $form->addRule('confirmemail','Email confirmation is required','required');
- $form->addRule('confirmemail', 'Email is incorrect', 'checkmailDNS', true);
- $form->addRule('email','Email is already in use on the system', 'callback', 'checkEmailfromDB');
- $form->addRule(array('email', 'confirmemail'), 'Error: Email mismatch', 'compare');
- //***********************************************************************************************************
- //Form Filters*********************************************************************************************
- $form->applyFilter('_ALL_', 'trim');
- $form->applyFilter('firstname', 'lettersonly');
- $form->applyFilter('firstname', 'strtolower');
- $form->applyFilter('firstname', 'ucfirst');
- $form->applyFilter('lastname', 'lettersonly');
- $form->applyFilter('lastname', 'strtolower');
- $form->applyFilter('lastname', 'ucfirst');
- $form->applyFilter('email', 'strtolower');
- $form->applyFilter('confirmemail', 'strtolower');
- //Display Form************************************************************
- if ($form->validate()) {
- $form->freeze();
- }
- $form->display();
- ?>
- </body>
- </html>
- if ($form->validate()) {
- $user = new DataObjects_User;
- $user->username = $form->exportValue('username');
- if($user->find()) {
- // username is already exists in the db
- }
- }
- if ($form->validate()) {
- $username = $form->exportValue('username');
- $email = $form->exportValue('password');
- if( !checkusernamefromDB($username) or !checkEmailfromDB($email) ) {
- $form->display();
- } else {
- $sql = "INSERT INTO users (userID,userEMail) VALUES($username, $email)";
- $pdo->query($sql);
- }
- }