欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

關(guān)于ThinkPhp 框架表單驗(yàn)證及ajax驗(yàn)證問題

 更新時(shí)間:2017年07月19日 15:36:25   作者:陳山河z  
tp數(shù)據(jù)驗(yàn)證有兩種方式,一種是靜態(tài)方式,一種是動態(tài)方式,下面小編給大家?guī)砹薚hinkPhp 框架表單驗(yàn)證及ajax驗(yàn)證問題,感興趣的朋友一起看看吧

之前的表單驗(yàn)證都是用js寫的,這里也可以使用tp框架的驗(yàn)證。但是兩者比較而言還是js驗(yàn)證比較好,因?yàn)閠p框架驗(yàn)證會運(yùn)行后臺代碼,這樣運(yùn)行速度和效率就會下降。 

  自動驗(yàn)證是ThinkPHP模型層提供的一種數(shù)據(jù)驗(yàn)證方法,可以在使用create創(chuàng)建數(shù)據(jù)對象的時(shí)候自動進(jìn)行數(shù)據(jù)驗(yàn)證。驗(yàn)證的代碼要寫在模型層即Model里面。

  數(shù)據(jù)驗(yàn)證有兩種方式:

靜態(tài)方式:在模型類里面通過$_validate屬性定義驗(yàn)證規(guī)則。靜態(tài)方式定義好以后其它地方都可以使用。

動態(tài)方式:使用模型類的validate方法動態(tài)創(chuàng)建自動驗(yàn)證規(guī)則。動態(tài)方式比較靈活,哪里使用就寫,其它地方不可以使用。

無論是什么方式,驗(yàn)證規(guī)則的定義是統(tǒng)一的規(guī)則,定義格式為:

<?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());
}
}
} 
}

2.在thinkphp\Application\Home\View\Test寫上對應(yīng)的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>無標(biāo)題文檔</title>
</head>
<style type="text/css">
*{ font-family:微軟雅黑; padding:0px; margin:0px auto}
</style>
<body>
<form action="__ACTION__" method="post">
<div>用戶名:<input type="text" name="uid" /></div>
<div>密碼:<input type="text" name="pwd" /></div>
<div>確認(rèn)密碼:<input type="text" name="pwd1" /></div>
<div>姓名:<input type="text" name="name" /></div>
<div>郵箱:<input type="text" name="email" /></div>
<div>年齡:<input type="text" name="age" /></div>
<div><input type="submit" value="提交" /></div>
</form>
</div>
</body>
</html>

3.在thinkphp\Application\Home\Model里面寫模型文件,也就是驗(yàn)證的方法。

<?php
namespace Home\Model;
use Think\Model;
class YongHuuModel extends Model
{
protected $tablePrefix = "";
protected $trueTableName = 'yonghuu'; //真實(shí)表名
//protected $patchValidate = true;
protected $_validate = array(
array('uid','require','用戶名不能為空!'),
array('pwd','pwd1','兩次輸入的密碼不一致!',0,'confirm'), //兩個(gè)字段是否相同
array('email','email','郵箱格式不正確'),
array('name','/^[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)$/','身份證號不正確!',0,'regex'),
array('age','18,50','年齡不在范圍內(nèi)',0,'between'),
);
}

二、動態(tài)驗(yàn)證

1.在Application\Home\Controller里面寫方法

<?php
namespace Home\Controller;
use Think\Controller;
class TestController extends Controller
{
  public function add()
  {
    if(empty($_POST))//如果post數(shù)組為空
    {
      $this->show();//顯示add.html頁面
    }
    else//如果post數(shù)組不為空
    {
      $y = D("YongHu");
      $arr = array(//動態(tài)驗(yàn)證就是需要在哪驗(yàn)證就在哪里寫驗(yàn)證方法。
        array("uid","require","用戶名不能為空",0),//講驗(yàn)證的方法寫在方法里面
      );
      if($y->validate($arr)->create())//這里要先調(diào)用validate方法,然后將寫的驗(yàn)證方法放到validate里面
      {
        $y->add();
      }
      else
      {
        die($y->getError());
      }
    }
  }
}

2.在thinkphp\Application\Home\View\Test寫上對應(yīng)的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>無標(biāo)題文檔</title>
<style type="text/css">
</style>
</head>
<body>
  <form action="__ACTION__" method="post">
    <div>用戶名:<input type="text" name="uid" /></div>
    <div>密碼:<input type="text" name="pwd" /></div>
    <div>確認(rèn)密碼:<input type="text" name="pwd1" /></div>
    <div>姓名:<input type="text" name="name" /></div>
    <div>郵箱:<input type="text" name="email" /></div>
    <div>年齡:<input type="text" name="age" /></div>
    <div><input type="submit" value="提交" /></div>
  </form>
</body>
<script type="text/javascript">
</script>
</html>

3.在thinkphp\Application\Home\Model里面寫模型文件。

<?php
namespace Home\Model;
use Think\Model;
class YongHuModel extends Model
{
  protected $tablePrefix = "";//表示表格前綴為空,就是沒有前綴。
  protected $trueTableName = "yonghu";//如果不寫這句話,會自動去找Yong_Hu這張表,這是默認(rèn)的表格的命名。這里要寫上實(shí)際的表格的名字。
}

三、Ajax做驗(yàn)證

tp動態(tài)驗(yàn)證和靜態(tài)驗(yàn)證都有一個(gè)很大的缺點(diǎn),那就是在提示錯誤信息的時(shí)候都要跳轉(zhuǎn)到其它頁面顯示出錯誤信息。如果需要在當(dāng)前頁面顯示出錯誤信息,就需要用ajax做驗(yàn)證。

1.寫顯示和ajax處理方法

<?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(//動態(tài)驗(yàn)證就是需要在哪驗(yàn)證就在哪里寫驗(yàn)證方法。
        array("uid","require","用戶名不能為空"),//講驗(yàn)證的方法寫在方法里面
      );
    if($y->validate($arr)->create())//這里要先調(diào)用validate方法,然后將寫的驗(yàn)證方法放到validate里面
      {
        $this->ajaxReturn("通過驗(yàn)證","eval");
      }
      else
      {
        $this->ajaxReturn($y->getError(),"eval");
      }
  }
}

2.寫顯示頁面

<!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>無標(biāo)題文檔</title>
<style type="text/css">
</style>
</head>
<body>
    <div>用戶名:<input id="uid" type="text" name="uid" /></div>
    <div><input id="btn" type="button" value="驗(yàn)證" /></div>
</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>

總結(jié)

以上所述是小編給大家介紹的關(guān)于ThinkPhp 框架表單驗(yàn)證及ajax,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論