分类:
2007-03-15 10:57:43
关键词:PEAR, 表单构建类,多页表单处理 选择自 jxyuhua 的 Blog
PHP PEAR中的HTML_QuickForm_Controller可以轻松地实现多页面处理,比如注册页面,向导等有多个页面的情况,它由:
HTML_QuickForm_Controller this class extracts the action name from request and calls the appropriate handler. It includes several Pages
HTML_QuickForm_Page 页面显示
HTML_QuickForm_Action 逻辑代码的处理
三部分构成。
当处理单个页面的时候,你会觉得HTML_QuickForm_Controller的代码会比HTML_QuickForm更冗长,但是当有多个这样的页面(注册,安装向导等),你要做的就仅仅是增加几个HTML_QuickForm_Page,的子类,处理基于HTML_QuickForm_Action的事件(EVENT),然后将它们添加到HTML_QuickForm_Controller实例中。如果不用HTML_QuickForm_Controller,你也许需要写出一大堆的代码来实现。
下面我把前几天用HTML_QuickForm实现的一个注册页面
用HTML_QuickForm_Controller重新实现一遍,这次我将注册页面分为两个页面,另外最后的注册信息会保存到数据库中,这几天对于它的了解有了一点提高,相信代码会比上次的好。
需求:
1.建一个userinfo表
2.PEAR DB,HTML_QuickForm_Controller Package
3.其它的文件可以到以上的链接PHP PEAR/HTML/QuickForm中查看(css.css)
/**
* 完整的用户注册程序(多页面)
* 采用PEAR HTML_QuickForm_Controller + DB实现
* @author jxyuhua at gmail.com
* @date 2005-02-25
*/
require_once 'HTML/QuickForm/Controller.php';
require_once 'HTML/QuickForm/Action/Display.php';
require_once 'Connect.php';//自定义的数据连接文件
session_start();
/**
* 从数据库中判断用户(email)是否已经存在
*/
function checkUser($fields) {
global $db;
$sql = "select count(*) from userinfo where email = '" . $db->escapeSimple($fields['email']) . "'";
$number = $db->getOne($sql);
if(DB::isError($db)) {
die($db->getMessage());
}
if($number == 1) {
Return array('email' => '此Email已经存在,请重新输入!');
}
Return TRUE;
}
/**
* 基本信息页面
*/
class Page_Register_First extends HTML_QuickForm_Page
{
function buildForm()
{
$this->_formBuilt = true;
$this->addElement('header', null, '用户注册--基本资料');
$this->addElement('text', 'email', '用户ID(EMAIL):', array('size' => 20, 'maxlen' => 30));
$this->addElement('password', 'password1', '密码:', array('size' => 20, 'maxlen' => 20));
$this->addElement('password', 'password2', '重复输入密码:', array('size' => 20, 'maxlen' => 30));
$this->addElement('submit', 'submit', '下一步 >>');
$this->applyFilter('email', 'trim');
$this->addRule('email', '请输入正确的Email地址', 'required');
$this->addRule('password1', '密码最少应该为6位数字', 'required');
$this->addRule('password2', '密码最少应该为6位数字', 'required');
$this->addRule('email', '请输入正确的EMAIL地址', 'email');
$this->addRule('password1', '密码最少应该为6位数字', 'minlength', 6);
$this->addRule(array('password1', 'password2'), '2次输入的密码不相同,请重新输入', 'compare');
//$this->registerRule('email', 'function', 'checkUser');
//$this->addRule('email', 'Email已经存在,请重新选择', 'email');
$this->addFormRule('checkUser');
$this->setDefaultAction('next');
}
}
/**
* 详细资料页面
*/
class Page_Register_Second extends HTML_QuickForm_Page
{
function buildForm()
{
$this->_formBuilt = true;
$country = array('1' => '中国', '2' => '日本', '3' => '美国', '4' => '英国', '0' => '其它');
$city = array('1' => '北京', '2' => '广东', '3' => '江西', '0' => '其它');
$industry = array('1' => '学生', '2' => '科研机构', '3' => 'IT产业', '0' => '其它');
$from = array('1' => '网吧', '2' => '单位', '3' => '家里', '0' => '其它');
$this->addElement('header', null, '用户注册--详细资料');
$this->addElement('text', 'alias', '呢称:', array('size' => 20, 'maxlen' => 30));
$this->addElement('text', 'question', '密码提示问题:', array('size' => 30, 'maxlen' => 30));
$this->addElement('text', 'answer', '密码提示答案:', array('size' => 30, 'maxlen' => 30));
$this->addElement('textarea', 'description', '个人描述信息:', array('rows' => 3, 'cols' => 50, 'class' => 'textBox'));
$radio[] = &$this->createElement('radio', null, null, '以下信息对不外公开', '1');
$radio[] = &$this->createElement('radio', null, null, '以下信息对外公开', '0');
$this->addGroup($radio, 'secret');
$this->addElement('text', 'idnumber', '证件号码:', array('size' => 30, 'maxlength' => 30));
$radio2[] = $this->createElement('radio', 'idtype', null, '身份证', null, array('value' => 1, 'checked' => 'true'));
$radio2[] = $this->createElement('radio', 'idtype', null, '其它证件', null, array('value' => 2));
$this->addGroup($radio2);
$this->addElement('text', 'realname', '真实姓名:', array('size' => 20, 'maxlength' => 30));
$this->addElement('select', 'gender', '性别:', array('male' => '男', 'female' => '女'));
$this->addElement('date', 'date', '出生于:', array('format' => 'Y 年m 月d', 'minYear' => 1940, 'maxYear' => 1995));
$this->addElement('select', 'country', '所在国家:', $country);
$this->addElement('select', 'city', '省(市):', $city);
$this->addElement('text', 'town', '市(县):', array('size' => 20, 'maxlength' => 20));
$this->addElement('text', 'address', '联系地址:', array('size' => 50, 'maxlength' => 50));
$this->addElement('text', 'zipcode', '邮政编码:', array('size' => 20, 'maxlength' => 20));
$this->addElement('text', 'phone', '联系电话:', array('size' => 20, 'maxlength' => 20));
$this->addElement('text', 'company', '所在单位:', array('size' => 30, 'maxlength' => 30));
$this->addElement('text', 'department', '所在部门:', array('size' => 20, 'maxlength' => 20));
$this->addElement('select', 'industry', '所在行业:', $industry);
$this->addElement('select', 'source', '主要在哪里
访问我们的网站:', $from);
$this->addElement('text', 'webpage', '个人主页:', array('size' => 50, 'maxlength' => 50));
$prevnext[] =& $this->createElement('submit', $this->getButtonName('back'), '<< 返回');
$prevnext[] =& $this->createElement('submit', $this->getButtonName('next'), '注册');
$this->addGroup($prevnext, 'buttons', '', ' ', false);
$this->addRule('alias', '用户登录昵称必须填写!!', 'required');
$this->addRule('question', '请填写密码提示问题', 'required');
$this->addRule('answer', '请填写密码提示答案', 'required');
$this->addRule('idnumber', '请填写证件号码', 'required');
$this->addRule('realname', '请填写真实姓名', 'required');
$this->addRule('description', '个人描述不能超过255个字符', 'maxlength', 255);
$this->addRule('gender', '性别不能为空', 'required');
$this->addRule('born', '出生日期不能为空', 'required');
$this->addRule('country', '国家不能为空', 'required');
$this->addRule('city', '城市不能为空', 'required');
$this->addRule('address', '请填写你的联系地址', 'required');
$this->addRule('idnuber', '证件号码不能少于5位', 'minlength', 5);
$this->addRule('idnuber', '证件号码只能是英文字母或数字', 'alphanumeric');
$this->setDefaultAction('next');
}
}
/**
* 页面显示模板
*/
class Action_Display extends HTML_QuickForm_Action_Display
{
function _renderForm(& $page)
{
$renderer =& $page->defaultRenderer();
$page->setRequiredNote('* 为必填项.');
$renderer->setHeaderTemplate(<<<_HTML
{header}
_HTML
);
$renderer->setFormTemplate(<<<_HTML
>
{content}
_HTML
);
$renderer->setElementTemplate(<<<_HTML
*{label}
{error}
{element}
_HTML
);
$page->accept($renderer);
echo $renderer->toHtml();
}
}
/**
* 处理注册(存入数据库)
*/
class Action_Process extends HTML_QuickForm_Action
{
function perform(&$page, $actionName)
{
global $db;
$values = $page->controller->exportValues();
//insert into db
$table_name = 'userinfo';
$fields_values = array(
'email' => $values['email'],
'pass' => $values['password1'],
'alias' => $values['alias'],
'realname' => $values['realname'],
'question' => $values['question'],
'answer' => $values['answer'],
'description' => $values['description'],
'secret' => $values['secret'],
'idnumber' => $values['idnumber'],
'idtype' => $values['idtype'],
'gender' => $values['gender'],
'born' => implode('-', $values['date']),
'country' => $values['country'],
'city' => $values['city'],
'town' => $values['town'],
'address' => $values['address'],
'zipcode' => $values['zipcode'],
'phone' => $values['phone'],
'company' => $values['company'],
'department' => $values['department'],
'industry' => $values['industry'],
'come' => $values['source'],
'website' => $values['webpage']);
$res = $db->autoExecute($table_name, $fields_values, DB_AUTOQUERY_INSERT);
if (PEAR::isError($res)) {
echo($res->getMessage());
session_unset();
header('location:' . $_SERVER['PHP_SELF']);
} else {
session_unset();
header('Location:welcome.htm');
}
}
}
$register = & new HTML_QuickForm_Controller('register', true);
$register->addPage(new Page_Register_First('reg1'));
$register->addPage(new Page_Register_Second('reg2'));
$register->setDefaults(array('idtype' => 1,
'secret' => 1));
$register->addAction('display', new Action_Display());
$register->addAction('process', new Action_Process());
$register->run();
?>
我也是摸着石头过河,大家要有兴趣,可以和我讨论。