2012-02-26 28 views
1

我有以下代碼至極是PEAR的HTML,快速表單生成用戶註冊表單:PEAR HTML_Quick表單功能和處理數據

<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, '&nbsp;'); 

     //*********************************************************************************************************** 

     //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> 

什麼是正確的方法來檢查,如果一個值已經存在數據庫會顯示一條消息(如果有的話) - 作爲驗證規則,重要的是 - 處理數據並將其插入數據庫的方式是什麼?我對PHP非常陌生,對PEAR來說更是如此,我看過一些手冊,但他們似乎沒有說明submited數據是如何處理後綴的,可能太新手了...... D

回答

0

我認爲你可以做到這一點通過幾種不同的方式,例如:

(1)PEAR數據對象:

if ($form->validate()) { 
    $user = new DataObjects_User; 
    $user->username = $form->exportValue('username'); 
    if($user->find()) { 
     // username is already exists in the db 
    } 

} 

(2)PDO

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); 
    } 

} 

我希望上面的例子會給一個想法如何檢查數據庫中是否存在一個值。有一個非常好的教程如何使用PEAR HTML_QuickForm:http://devzone.zend.com/1165/generating-and-validating-web-forms-with-pear-html_quickform-part-2/

+0

Vasil,謝謝你的回答,這兩個例子是如何實現的,我已經發布了codewise的形式,是要安裝的PEAR包和第二個問題驗證後如何在數據庫中插入數據?我是新來的PHP,因爲你最注意的是。順便說一下,你是來自保加利亞嗎?;) – mookar 2012-02-26 15:46:37

+0

是的,我來自保加利亞;) – 2012-02-26 16:08:02

+0

如果你沒有安裝MDB2,你可以從命令行安裝它。您還需要安裝相應DBMS的驅動程序才能進行安裝: $ pear install MDB2 $ pear install MDB2#mysql 之後,您應該檢查MDB2文檔:http://pear.php。淨/手動/ EN/package.database.mdb2.php – 2012-02-26 16:18:57