Detailed explanation of ThinkPhp framework form verification and ajax verification examples

小云云
Release: 2023-03-19 21:58:01
Original
1145 people have browsed it

There are two ways to verify tp data, one is static and the other is dynamic. The previous form verification was written in js. The verification of tp framework can also be used here. But comparing the two, js verification is better, because tp framework verification will run background code, so the running speed and efficiency will decrease. Automatic verification is a data verification method provided by the ThinkPHP model layer, which can automatically perform data verification when using create to create a data object. The verification code must be written in the model layer, that is, the Model.

There are two methods of data validation:

Static method: Define validation rules through the $_validate attribute in the model class. After the static method is defined, it can be used elsewhere.

Dynamic method: Use the validate method of the model class to dynamically create automatic validation rules. The dynamic method is more flexible. It can be written wherever it is used and cannot be used elsewhere.

No matter what method is used, the definition of verification rules is a unified rule, and the definition format is:

<?php
namespace Home\Controller;
use Think\Controller;
class TestController extends Controller
{
public function add()
{
if(empty($_POST))
{ 
$this->show();
}
else
{ 
$y=new \Home\Model\YongHuuModel();
$r=$y->create();
if($r)
{
$y->add(); 
}
else{
die($y->getError());
}
}
} 
}
Copy after login

2. Write the corresponding html file in thinkphp\Application\Home\View\Test

<!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>
</head>
<style type="text/css">
*{ font-family:微软雅黑; padding:0px; margin:0px auto}
</style>
<body>
<form action="__ACTION__" method="post">
<p>用户名:<input type="text" name="uid" /></p>
<p>密码:<input type="text" name="pwd" /></p>
<p>确认密码:<input type="text" name="pwd1" /></p>
<p>姓名:<input type="text" name="name" /></p>
<p>邮箱:<input type="text" name="email" /></p>
<p>年龄:<input type="text" name="age" /></p>
<p><input type="submit" value="提交" /></p>
</form>
</p>
</body>
</html>
Copy after login

3. Write the model file in thinkphp\Application\Home\Model, which is the verification method.

<?php
namespace Home\Model;
use Think\Model;
class YongHuuModel extends Model
{
protected $tablePrefix = "";
protected $trueTableName = &#39;yonghuu&#39;; //真实表名
//protected $patchValidate = true;
protected $_validate = array(
array(&#39;uid&#39;,&#39;require&#39;,&#39;用户名不能为空!&#39;),
array(&#39;pwd&#39;,&#39;pwd1&#39;,&#39;两次输入的密码不一致!&#39;,0,&#39;confirm&#39;), //两个字段是否相同
array(&#39;email&#39;,&#39;email&#39;,&#39;邮箱格式不正确&#39;),
array(&#39;name&#39;,&#39;/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/&#39;,&#39;身份证号不正确!&#39;,0,&#39;regex&#39;),
array(&#39;age&#39;,&#39;18,50&#39;,&#39;年龄不在范围内&#39;,0,&#39;between&#39;),
);
}
Copy after login

2. Dynamic verification

1. Write the method in Application\Home\Controller

<?php
namespace Home\Controller;
use Think\Controller;
class TestController extends Controller
{
  public function add()
  {
    if(empty($_POST))//如果post数组为空
    {
      $this->show();//显示add.html页面
    }
    else//如果post数组不为空
    {
      $y = D("YongHu");
      $arr = array(//动态验证就是需要在哪验证就在哪里写验证方法。
        array("uid","require","用户名不能为空",0),//讲验证的方法写在方法里面
      );
      if($y->validate($arr)->create())//这里要先调用validate方法,然后将写的验证方法放到validate里面
      {
        $y->add();
      }
      else
      {
        die($y->getError());
      }
    }
  }
}
Copy after login

2. Write the corresponding method in thinkphp\Application\Home\View\Test html file

<!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">
</style>
</head>
<body>
  <form action="__ACTION__" method="post">
    <p>用户名:<input type="text" name="uid" /></p>
    <p>密码:<input type="text" name="pwd" /></p>
    <p>确认密码:<input type="text" name="pwd1" /></p>
    <p>姓名:<input type="text" name="name" /></p>
    <p>邮箱:<input type="text" name="email" /></p>
    <p>年龄:<input type="text" name="age" /></p>
    <p><input type="submit" value="提交" /></p>
  </form>
</body>
<script type="text/javascript">
</script>
</html>
Copy after login

3. Write the model file in thinkphp\Application\Home\Model.

<?php
namespace Home\Model;
use Think\Model;
class YongHuModel extends Model
{
  protected $tablePrefix = "";//表示表格前缀为空,就是没有前缀。
  protected $trueTableName = "yonghu";//如果不写这句话,会自动去找Yong_Hu这张表,这是默认的表格的命名。这里要写上实际的表格的名字。
}
Copy after login

3. Ajax verification

tp dynamic verification and static verification both have a big shortcoming, that is, when an error message is prompted, they have to jump to other pages to display the error. information. If you need to display an error message on the current page, you need to use ajax for verification.

1. Write display and ajax processing methods

<?php
namespace Home\Controller;
use Think\Controller;
class TestController extends Controller
{
  public function tianjia()//添加方法,用来显示页面
  {
    $this->show();
  }
  public function test()//ajax处理方法
  {
    $y = D("YongHu");
    $arr = array(//动态验证就是需要在哪验证就在哪里写验证方法。
        array("uid","require","用户名不能为空"),//讲验证的方法写在方法里面
      );
    if($y->validate($arr)->create())//这里要先调用validate方法,然后将写的验证方法放到validate里面
      {
        $this->ajaxReturn("通过验证","eval");
      }
      else
      {
        $this->ajaxReturn($y->getError(),"eval");
      }
  }
}
Copy after login

2. Write display page

<!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" />
<script src="__PUBLIC__/js/jquery-1.11.2.min.js"></script>
<title>无标题文档</title>
<style type="text/css">
</style>
</head>
<body>
    <p>用户名:<input id="uid" type="text" name="uid" /></p>
    <p><input id="btn" type="button" value="验证" /></p>
</body>
<script type="text/javascript">
  $("#btn").click(function(){
      var uid = $("#uid").val();
      $.ajax({
        url:"__CONTROLLER__/test",
        data:{uid:uid},
        type:"POST",
        dataType:"TEXT",
        success: function(data){
            alert(data);
          }        
        })
    })
</script>
</html>
Copy after login

Related recommendations:

PHP users Verify PHP+Ajax verification code to verify user login

Ajax verification username example code

ajax verification username and password, ajax verification username_ PHP tutorial

The above is the detailed content of Detailed explanation of ThinkPhp framework form verification and ajax verification examples. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template