ThinkPHP Form Automatic Submission Verification Example Tutorial

  • 2021-07-09 07:17:39
  • OfStack

This paper describes the implementation process of ThinkPHP form automatic submission verification with an example. The detailed steps are as follows:

1. Template section:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<TITLE>ThinkPHP Examples: Form Submission, Automatic Validation, and Automatic Population </TITLE>
</HEAD>
<BODY>
<div class="main">
<FORM METHOD=POST ACTION="__URL__/insert">
<TABLE cellpadding=2 cellspacing=2>
<TR>
 <TD width="12%"> Title: </TD>
 <TD ><INPUT TYPE="text" NAME="title" style="height:23px" class="large bLeft"></TD>
</TR>
<TR>
 <TD > Mailbox: </TD>
 <TD ><INPUT TYPE="text" NAME="email" style="height:23px" class="large bLeft"></TD>
</TR>
<TR>
 <TD> Content: </TD>
 <TD><TEXTAREA NAME="content" class="large bLeft" ROWS="8" COLS="25"></TEXTAREA></TD>
</TR>
<TR>
 <TD> Verification code: </TD>
 <TD><INPUT TYPE="text" NAME="verify" style="height:23px" class="small" > <img src="__URL__/verify" align="absmiddle" />  Enter the corresponding number </TD>
</TR>
<TR>
 <TD></TD>
 <TD><INPUT TYPE="submit" class="button" value=" Mention   Hand in "> <INPUT TYPE="reset" class="button" value=" Qing   Empty "></TD>
</TR>
<TR>
<TD></TD>
 <TD ><HR></TD>
</TR>
<volist name="list" id="vo">
<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>
</volist>
</TABLE>
 </FORM>
</div>
</BODY>
</HTML>

2. Model part form class program code:


<?php 
class FormModel extends Model {
 // 自动验证设置
 protected $_validate  =  array(//这里必须定义为$_validata用来验证
 array('title','require','标题必须!',1),
 array('email','email','邮箱格式错误!',2),
 array('content','require','内容必须'),
 array('verify','require','验证码必须!'),
 array('verify','CheckVerify','验证码错误',0,'callback'),//callback 使用方法验证,前面定义的验证规则是1个当前 Model 类的方法 
//这里如果有不太理解的话可以看本类的最后1个方法
 array('title','','标题已经存在',0,'unique','add'),//附加验证unique,unique 验证是否唯1,系统会根据字段目前的值查询数据库来判断是否存在相同的值
 );
 /*think的验证机制,省去了不少麻烦非空:equire 邮箱:email 验证码正确:CheckVerify
 array()数组内容array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间)
 验证条件
 EXISTS_TO_VAILIDATE 或者 0 存在字段就验证 (默认)
 MUST_TO_VALIDATE 或者1 必须验证
 VALUE_TO_VAILIDATE 或者 2 值不为空的时候验证
 附加规则 配合验证规则使用,包括:
 function 使用函数验证,前面定义的验证规则是1个函数名
 callback 使用方法验证,前面定义的验证规则是1个当前 Model 类的方法
 confirm 验证表单中的两个字段是否相同,前面定义的验证规则是1个字段名
 equal 验证是否等于某个值,该值由前面的验证规则定义
 in 验证是否在某个范围内,前面定义的验证规则必须是1个数组
 unique 验证是否唯1,系统会根据字段目前的值查询数据库来判断是否存在相同的值
 regex 使用正则进行验证,表示前面定义的验证规则是1个正则表达式(默认)
 */
 // 自动填充设置
 //由上面推导,下面这个是自动填充字段了,方便明了
 protected $_auto  =  array(//同样这里必须定义为$_auto
 array('status','1','ADD'),
 array('create_time','time','ADD','function'),//这里指明填充使用函数time()
 );
 /*
 数组因子:
 array(填充字段,填充内容,填充条件,附加规则)
 填充条件包括:
 ADD 新增数据的时候处理(默认方式)
 Update 更新数据的时候处理
 ALL 所有情况下都进行处理
 
 附加规则包括:
 function 使用函数
 callback 回调方法
 field 用其它字段填充
 string 字符串(默认方式)
 */

 public function CheckVerify() {
  return md5($_POST['verify']) == $_SESSION['verify'];
 }
}
?>

3. indexaction program code of controller part:


<?php 
class IndexAction extends Action{
 //  Home page 
 public function index(){
  $Form = D("Form");// Create 1 Objects 
  $list = $Form->top6('','*','id desc');// Read the latest from the database 6 A record , And Ann id Reverse output 
  $this->assign('list',$list);// Pass the data to the template 
  $this->display();
 }
 //  Working with form data 
 public function insert() {// This method corresponds to the form's ACTION="__URL__/insert"
  $Form = D("Form");
  if($Form->create()) {// Create  Form  Data object, which is created by default from the data submitted by the form , Prepare for the following write to the database 
   $Form->add();//  Add data submitted by the form  , Submit the data object created above 
   $this->redirect();// Back up 1 Modules , Page jump can be said to be 
  }else{
   header("Content-Type:text/html; charset=utf-8");
   exit($Form->getError().' [ <A href="javascript:history.back()" rel="external nofollow" > Return   Gback </A> ]');
  }
 }
 //  Generate verification code 
 // There is nothing to say about this method , Fixed format 
 public function verify() {
  import("ORG.Util.Image");
   Image::buildImageVerify(); // Here, the two colons are calling static methods 
 }
} 
?>

Related articles: