本文以实例讲述了ThinkPHP表单自动提交验证的实现过程,详细步骤如下所示:
一、模板部分:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><TITLE>ThinkPHP示例:表单提交、自动验证和自动填充</TITLE><body><div class="main"><TABLE cellpadding="2" cellspacing="2"><TR> <TD width="12%">标题:</TD> <TD></TD></TR><TR> <TD>邮箱:</TD> <TD></TD></TR><TR> <TD>内容:</TD> <TD><TEXTAREA NAME="content" class="large bLeft" ROWS="8" COLS="25"></TEXTAREA></TD></TR><TR> <TD>验证码:</TD> <TD> 输入对应的数字</TD></TR><TR> <TD></TD> <TD> </TD></TR><TR><TD></TD> <TD><HR></TD></TR><TR><TD></TD> <TD style="border-bottom:1px dotted silver">{$vo.title} <span style="color:gray">[{$vo.email} {$vo.create_time|date='Y-m-d H:i:s',###}]</span></TD></TR><TR><TD></TD> <TD><div class="content">{$vo.content|nl2br}</div></TD></TR></TABLE> </div>
二、模型部分form类程序代码:
三、控制器部分indexaction类程序代码:
top6('','*','id desc');//从数据库中读取最新<a>2本文来源gao*daima.com搞@代#码&网6</a><pre>搞gaodaima代码
6条记录,并且安id倒序输出 $this->assign(‘list’,$list);//把数据传到模板里 $this->display(); } // 处理表单数据 public function insert() {//此方法对应表单的ACTION=”__URL__/insert” $Form = D(“Form”); if($Form->create()) {//创建 Form 数据对象,默认通过表单提交的数据进行创建,为下面写入数据库做准备 $Form->add();// 新增表单提交的数据 ,吧上面创建的数据对象提交 $this->redirect();//返回上一个模块,页面跳转可以说是 }else{ header(“Content-Type:text/html; charset=utf-8”); exit($Form->getError().’ [ 返 回 ]’); } } // 生成验证码 //这个方法没什么好说的,固定格式 public function verify() { import(“ORG.Util.Image”); Image::buildImageVerify(); //这里两个冒号是调用静态方法 }} ?>