今天给大家介绍的是当下很流行的框架layui中的一个小案例、就是form表单监控提交并且使用ajax异步提交验证数据。在layui中我们想使用哪个模块就要layui.use('form',function{});这种形式要引用form内置模板,下面是HTML全部代码
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>注册</title> <meta content="width=device-width, initial-scale=1, maximum-scale=1"> <meta content="雷小天"> <meta content="雷小天博客-layui中使用form表单监听异步验证注册"> <link href="https://www.jb51.net/__RES__/layui/css/layui.css" > <link href="https://www.jb51.net/__RES__/css/global.css" > </head> <body> <div> <div> <a href="{:U('device/getinfo')}" > <img src="https://www.jb51.net/__RES__/images/logo.png" alt="layui"> </a> <ul> <!-- 未登入的状态 --> <li> <a href="https://www.jb51.net/user/login.html" ></a> </li> <li> <a href="{:U('device/login')}" >登入</a> </li> <li> <a href="javascript::void;" >注册</a> </li> </ul> </div> </div> <div> <div pad20> <div lay-filter="user"> <ul> <li><a href="{:U('device/login')}" >登入</a></li> <li>注册</li> </ul> <div> <div> <div> <form method="post"> <div> <label for="L_username">手机</label> <div> <input type="text" required lay-verify="required" autocomplete="off"> </div> </div> <div> <label for="L_username">卡号</label> <div> <input type="text" required lay-verify="required" autocomplete="off"> </div> </div> <div> <button type="submit" lay-filter="reg" lay-submit="" >立即注册</button> </div> </form> </div> </div> </div> </div> </div> </div> <div> <p>开门社区 2018 © <a href="https://www.100txy.com/" target="_blank">100txy.com 出品</a></p> <p><a href="https://www.jb51.net/JavaScript::void;" target="_blank">雷小天博客</a></p> </div> <script src="https://www.jb51.net/__PUBLIC__/mobile/js/jquery.js"></script> <script src="https://www.jb51.net/__RES__/layui/layui.js"></script> <script> //监听提交 layui.use('form', function(){ var form = layui.form; form.on('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(!myreg.test(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'=>'恭喜你,注册成功!')); } } } }