layui中使⽤form表单监听ajax异步验证注册
今天给⼤家介绍的是当下很流⾏的框架layui中的⼀个⼩案例、就是form表单监控提交并且使⽤ajax异步提交验证数据。在layui中我们想使⽤哪个模块就要layui.use('form',function{});这种形式要引⽤form内置模板,下⾯是HTML全部代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>注册</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="keywords" content="雷⼩天">
<meta name="description" content="雷⼩天博客-layui中使⽤form表单监听异步验证注册">
<link rel="stylesheet" href="__RES__/layui/css/layui.css">
<link rel="stylesheet" href="__RES__/css/global.css">
</head>
<body>
<div class="fly-header layui-bg-black">
<div class="layui-container">
<a class="fly-logo" href="{:U('device/getinfo')}">
<img src="__RES__/images/logo.png" alt="layui">
</a>
<ul class="layui-nav fly-nav-user">
电容在电路中的作用<!-- 未登⼊的状态 -->
<li class="layui-nav-item">
<a class="iconfont icon-touxiang layui-hide-xs" href="user/login.html"></a>
</li>
苹果笔记本装xp<li class="layui-nav-item">
<a href="{:U('device/login')}">登⼊</a>
</li>
<li class="layui-nav-item">
<a href="javascript::void;">注册</a>
</li>
</ul>
</div>
</div>
<div class="layui-container fly-marginTop">
<div class="fly-panel fly-panel-user" pad20>
<div class="layui-tab layui-tab-brief" lay-filter="user">
<ul class="layui-tab-title">
<li><a href="{:U('device/login')}">登⼊</a></li>
<li class="layui-this">注册</li>
</ul>
<div class="layui-form layui-tab-content" id="LAY_ucm" >
<div class="layui-tab-item layui-show">
<div class="layui-form layui-form-pane">
<form method="post">
<div class="layui-form-item">
<label for="L_username" class="layui-form-label">⼿机</label>
<div class="layui-input-inline">
<input type="text" id="signuppassdata" name="phone" required lay-verify="required" autocomplete="off" class="layui-input">
</div>开机密码忘记怎么办
</div>
<div class="layui-form-item">
<label for="L_username" class="layui-form-label">卡号</label>
<div class="layui-input-inline">
<input type="text" id="cardno" name="cardno" required lay-verify="required" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<button class="layui-btn" type="submit" lay-filter="reg" lay-submit="" id="signuppass" >⽴即注册</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="fly-footer">
<p>开门社区 2018 © <a href="www.100txy/" target="_blank">100txy 出品</a></p>
<p><a href="JavaScript::void;" target="_blank">雷⼩天博客</a></p>
</div>
<script src="__PUBLIC__/mobile/js/jquery.js"></script>
<script src="__RES__/layui/layui.js"></script>
<script>
//监听提交
/
/监听提交
layui.use('form', function(){
var form = layui.form;
<('submit(reg)', function(data){
// layer.msg(JSON.stringify(data.field));//弹出json格式所有表单的值
var phone=data.field.phone;
var cardno=data.field.cardno;
if(phone.length!=11){
layer.msg('请输⼊有效的⼿机号码', {icon: 2});
return false;
}
var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
if(!st(phone)){
layer.msg('请输⼊有效的⼿机号码', {icon: 2});
return false;
}
if(cardno.length!=8){
layer.msg('请输⼊有效的卡⽚内码!', {icon: 2});
return false;
}
$.ajax({
type:"POST",
url:"index.php?m=Home&c=Device&a=ajax_reg",
data:"phone="+phone+"&cardno="+cardno,
dataType:"json",
success:function(data){
if(data.status=='yes'){
layer.msg('注册成功!', {icon: 1});
var url = "{:U('device/getinfo')}"; //成功跳转的url
setTimeout(window.location.href=url,2000);
}else{
layer.msg(data.msg, {icon: 2});
}
},
});
return false;
});
});
</script>
</body>
如何用硬盘重装系统</html>
下⾯是ajax_reg逻辑⽅⾯,这是我的业务逻辑⼤家没必要照搬,知道怎么⽤就⾏了
//表单异步提交
public function ajax_reg(){
if(isset($_POST['phone'])&&isset($_POST['cardno'])){
$roomnostr = M("userinfo")->where(array('cardno'=>$_POST['cardno']))->getField('roomno');
if(!$roomnostr){
echo json_encode(array('status'=>'no','msg'=>'⽆效的卡⽚内码,请联系管理员!'));
exit();
}
$wechatinfo=$_SESSION['wechatinfo'];
if(!$wechatinfo){
echo json_encode(array('status'=>'no','msg'=>'页⾯已过期请重新扫码进⼊!'));
exit();
}
$result= M("weixin")->where(array('openid'=>$wechatinfo['openid']))->field('is_signup,area_id')->find();
$areaArr=explode('-',$result['area_id']);
// $result=M("weixin")->getFieldByOpenid($_POST['openid'],'is_signup');
if(isset($result)&&$result['is_signup']==1){//如果注册过⼀次下次只更新门区
$new_area=$roomnostr;
$reg=M("weixin")->where(array('openid'=>$wechatinfo['openid']))->save(array('area_id'=>$new_area));
echo json_encode(array('status'=>'no','type'=>'signuppass','msg'=>'你已注册为该门区的租户,请勿重复注册!')); exit();
}else{
$data=array(
车震门事件'is_signup'=>1,
'phone'=>$_POST['phone'],
'openid'=>$wechatinfo['openid'],
'nickname'=>$wechatinfo['nickname'],
'sex'=>$wechatinfo['sex'],
'headimgurl'=>$wechatinfo['headimgurl'],
'area_id'=>$roomnostr,
'regtime'=>time(),
'cardno'=>$_POST['cardno'],
);
$reg=M("weixin")->add($data);
if($reg){
echo json_encode(array('status'=>'yes','msg'=>'恭喜你,注册成功!'));
}
}
}
孤雁 电视剧}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论