您好,登錄后才能下訂單哦!
今天給大家介紹的是當下很流行的框架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" rel="external nofollow" > <link rel="stylesheet" href="__RES__/css/global.css" rel="external nofollow" > </head> <body> <div class="fly-header layui-bg-black"> <div class="layui-container"> <a class="fly-logo" href="{:U('device/getinfo')}" rel="external nofollow" > <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" rel="external nofollow" ></a> </li> <li class="layui-nav-item"> <a href="{:U('device/login')}" rel="external nofollow" rel="external nofollow" >登入</a> </li> <li class="layui-nav-item"> <a href="javascript::void;" rel="external nofollow" >注冊</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')}" rel="external nofollow" rel="external nofollow" >登入</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 rel="external nofollow" target="_blank">100txy.com 出品</a></p> <p><a href="JavaScript::void;" rel="external nofollow" 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; 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'=>'恭喜你,注冊成功!')); } } } }
以上這篇在layui中使用form表單監聽ajax異步驗證注冊的實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。