Thinkphp 简单表单提交验证

具体的操作步骤如下:

第一步:入口文件index.php内容 (此文件基本是属于固定的格式)

<?php

define('THINK_PATH','./ThinkPHP/');

define('APP_NAME','MyApp');

define('APP_PAHT','./MyApp/');

require_once THINK_PATH.'ThinkPHP.php';

$app=new App();

$app->run();

?>

 

第二步:Active文件夹中的IndexAction.class.php文件内容

<?php

class IndexAction extends Action

{

 public function Index()

 {

  $this->display();//渲染到模板index.html

 }

 

 // 生成验证码  

 public function verify()//这是一个固定的格式

 {  

  import("ORG.Util.Image");  

  Image::buildImageVerify();  

 }  

 

 //检验验证码是否正确  

 public function verifyCheck()

 {     

  if (md5($_POST['verifyTest']) != Session::get('verify'))

  {  

   die('验证码错误');  //如果验证码不对就退出程序

  } 

 }   

 

  function insert()

  {

  header('Content-Type:text/html; charset=utf-8');//防止出现乱码

  $this->verifyCheck();//调用本类的函数,

  $Pagemodel = D("user");

  $vo = $Pagemodel->create();

  if(false === $vo) die($Pagemodel->getError());

  $topicid = $Pagemodel->add(); //add方法会返回新添加的记录的主键值

  if($topicid) echo "数据库添加成功";

  else throw_exception("数据库添加失败");

 }

}

?>

 

第三步:写模板文件,也就是写LIB文件夹中的HTML文件

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>无标题文档</title>

  <style type="text/css">

    #form1

 {

  width:250px;

  height:250px;

  margin:20px auto;

  border:1px #039 solid;

  padding:20px 20px;

 }

  </style>

  <script type='text/javascript'>

 function freshVerify()

 {  

   document.getElementByIdx_x('verifyImg').src='__URL__/verify/'+Math.random();  

 }  

  </script>  

</head>

<body>

  <form name="form1" id="form1" method="post" action="__URL__/insert">

    注册帐号:<br /><br />

    帐号:<input type="text" name="user" id="user" maxlength="16" /><br /><br />

    密码:<input type="password" name="password" id="password" maxlength="16" /><br /><br />

    Q&nbsp;&nbsp;Q:<input type="text" name="qq" id="qq" maxlength="16" /><br /><br />

   

    验证码:<input type='text' name='verifyTest' size="5"> 

    <img style='cursor:pointer' title='刷新验证码' src='__URL__/verify' id='verifyImg' onClick='freshVerify()'/> <br /><br />

      

    <input type="submit" name="btn1" id="btn1" value="提交" />

    <input type="reset" name="btn2" id="btn2" value="重置" />

  </form>

</body>

</html>

注意:

1、也就是一个form,action="__URL__/insert"表示提交到当前Action类(即IndexAction.class.php文件中)的insert函数;

2、此模板(静态网页)中的各个name要与user数据表的各个字段是一样的名字,否则在insert函数中数据不能自动进库。

3、验证码的刷新由静态网页负责。值相等由IndexAction类的verifyCheck()负责。

 

第四步:写Model类,在model目录中,文件名为:UserModel.class.php

<?php

  class UserModel extends Model//User与数据库中数据表的名字相同

  {

   var $_validate=array  //自动验证

   (

   array('user','require','账号不能为空',1),  //1表示必须验证

   array('qq','number','QQ号必须是数字,注册失败!',2),//2表示不为空时要验证

   array('user','','此帐号己经存在!',0,'unique','add')  //不能有同时账号出现

   );   

   var $_auto=array

   (

         array('password','md5','add','function'),  //密码用md5加密后填入数据表中

         array('create_time','time','add','function')  //在增加时自动将时间擢填入表中

   );

  }

?>

注解:

1、文件名,类名必须用user,因为数据库中对应的是user表;

2、其实只要写一个框架就行了:

  class UserModel extends Model

  {

  }

但为什么还要var $_validate=array()和var $_auto=array()呢?那是因为:

var $_validate=array()是自动验证的意思,var $_auto=array()是自动填充的意思。

自动验证就是验证数据的格式是否正确,自动填充就是你不输入的值,它自动给你灌进去,比如'create_time'建立时间,我们在模板中没有这个,但这里它就自动进库了。


转载于:https://my.oschina.net/leohouse/blog/651154

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值