關(guān)于ThinkPhp 框架表單驗(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)站的支持!
- ThinkPHP框架安全實(shí)現(xiàn)分析
- thinkphp微信開之安全模式消息加密解密不成功的解決辦法
- thinkphp5.1框架容器與依賴注入實(shí)例分析
- 對于ThinkPHP框架早期版本的一個(gè)SQL注入漏洞詳細(xì)分析
- ThinkPHP表單自動提交驗(yàn)證實(shí)例教程
- thinkPHP實(shí)現(xiàn)表單自動驗(yàn)證
- ThinkPHP表單自動驗(yàn)證實(shí)例
- Thinkphp框架 表單自動驗(yàn)證登錄注冊 ajax自動驗(yàn)證登錄注冊
- ThinkPHP框架表單驗(yàn)證操作方法
- ThinkPHP 表單自動驗(yàn)證運(yùn)用示例
- thinkPHP自動驗(yàn)證、自動添加及表單錯誤問題分析
- ThinkPHP中create()方法自動驗(yàn)證表單信息
- TP5框架安全機(jī)制實(shí)例分析
相關(guān)文章
淺析echo(),print(),print_r(),return之間的區(qū)別
這篇文章主要是對echo(),print(),print_r(),return之間的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下,希望對大家有所幫助2013-11-11PHP 文本文章分頁代碼 按標(biāo)記或長度(不涉及數(shù)據(jù)庫)
PHP文本分頁,按標(biāo)記或者長度分頁,非傳統(tǒng)的數(shù)據(jù)庫分頁。廢話不多說,客觀如有意可直接看代碼2012-06-06php測試程序運(yùn)行速度和頁面執(zhí)行速度的代碼
microtime()函數(shù)返回當(dāng)前 Unix 時(shí)間戳的微秒數(shù)。用于檢測程序執(zhí)行時(shí)間的函數(shù),也是PHP內(nèi)置的時(shí)間函數(shù)之一,在PHP中可以用于對程序執(zhí)行時(shí)間的判斷,以及相同功能函數(shù)的執(zhí)行效率高低快慢的判斷。2022-12-12關(guān)于Yii2框架跑腳本時(shí)內(nèi)存泄漏問題的分析與解決
這篇文章主要給大家介紹了關(guān)于Yii2框架跑腳本時(shí)內(nèi)存泄漏問題的分析與解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Yii2具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12linux實(shí)現(xiàn)php定時(shí)執(zhí)行cron任務(wù)詳解
linux實(shí)現(xiàn)php定時(shí)執(zhí)行cron任務(wù)2013-12-12