《PHP實例:Thinkphp框架 表單自動驗證登錄注冊 ajax自動驗證登錄注冊》要點:
本文介紹了PHP實例:Thinkphp框架 表單自動驗證登錄注冊 ajax自動驗證登錄注冊,希望對您有用。如果有疑問,可以聯系我們。
相關主題:thinkphp教程
PHP實戰動態驗證:(不需要建Model模型)
PHP實戰1.建一個控制器,做表單操作(包含驗證)
PHP實戰
<?php
namespace Biaodan\Controller;
use Think\Controller;
class BiaodanController extends Controller
{
public function test()
{
if(empty($_POST))//如果$_POST空,顯示添加頁面,
{
$this->show();
}
else //如果$_POST不為空,走驗證,驗證是否成功,添加數據庫
{
$y = D("yonghu");
$arr = array(//造一個驗證規則
array(‘uid‘,‘require‘,‘用戶名不能為空!‘,0),
array(‘pwd‘,‘pwd1‘,‘輸入的密碼不一致‘,0,‘confirm‘),//相等驗證:confirm(驗證表單中的兩個字段是否相同,定義的驗證規則是一個字段名),意思是pwd1必須為字段
array(‘email‘,‘email‘,‘郵箱格式不正確‘,0),
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‘,‘年齡不在范圍內‘,0,‘between‘)//范圍驗證
);
if($y->validate($arr)->create())//$y->validate($arr),添加之前執行驗證規則
{
$y->add();
}
else
{
die($y->getError());
}
}
}
}
PHP實戰2.對應控制器方法的html頁面:
PHP實戰
<!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>
<body>
<form action="__ACTION__" method="post">
<br />
<br />
<br />
<br />
<div>用戶名:<input type="text" name="uid" /></div>
<br />
<div>密碼:<input type="text" name="pwd" /></div>
<br />
<div>確認密碼:<input type="text" name="pwd1" /></div>
<br />
<div>姓名:<input type="text" name="name" /></div>
<br />
<div>郵箱:<input type="text" name="email" /></div>
<br />
<div>年齡:<input type="text" name="age" /></div>
<br />
<div><input type="submit" value="添加" /></div>
</form>
</body>
</html>
PHP實戰ajax:(不需要建Model模型)
PHP實戰1.建一個控制器,做表單操作(包含驗證)
PHP實戰
<?php
namespace Admin\Controller;
use Think\Controller;
class ZhuCeController extends Controller {
public function add(){
$this->show();
}
public function addchuli(){
$y = D("yonghubiao");
$arr = array(
array(‘uid‘,‘require‘,‘用戶名不能為空!!‘),//ps: require 意思是字段必須,就是不能為空
array(‘pwd‘,‘require‘,‘兩次密碼不一致!‘),
array(‘name‘,‘require‘,‘沒有填寫名稱!!‘),
array(‘email‘,‘email‘,‘郵箱格式不正確!!‘),
array(‘age‘,‘/^[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‘),
);
if($y->validate($arr)->create()){
$y->add();
$this->ajaxReturn("注冊成功","eval");
}else{
$this->ajaxReturn($y->getError(),"eval");
}
}
}
PHP實戰2.對應控制器方法的html頁面:
PHP實戰
<!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>
</head>
<body>
<br />
<br />
<br />
<div>用戶名:<input type="text" id="uid" /></div>
<br />
<div>?密碼:<input type="text" id="pwd" /></div>
<br />
<div>?名稱:<input type="text" id="name" /></div>
<br />
<div>?郵箱:<input type="text" id="email" /></div>
<br />
<div>身份證號:<input type="text" id="age" /></div>
<br />
<div><input type="button" id="dtn" value="提交" /></div>
</body>
<script type="text/javascript">
$("#dtn").click(function(){
var uid = $("#uid").val();
var pwd = $("#pwd").val();
var name = $("#name").val();
var email = $("#email").val();
var age = $("#age").val();
$.ajax({
url:"__CONTROLLER__/addchuli",
data:{uid:uid,pwd:pwd,name:name,email:email,age:age},
dataType:"TEXT",
type:"POST",
success: function(data){
alert(data);
}
});
});
</script>
</html>
PHP實戰以上所述是小編給大家介紹的Thinkphp框架 表單自動驗證登錄注冊 ajax自動驗證登錄注冊,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的.在此也非常感謝大家對維易PHP網站的支持!
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/2230.html