HTML
<div class="getbox"> <span class="navt" tip="手机验证码"> <span>手机验证码</span>:</span> <input class="getphone nokong" type="text" name="yzm" id="yzm" value="" placeholder="请输入验证码"/> <input type="hidden" name="openid_start" value="{$openid_start}"/> <input type="hidden" name="id" value="{$id}"/> <input id="jingdu" name="jingdu" type="hidden" > <input id="weidu" name="weidu" type="hidden" > <input id="dizhi" name="dizhi" type="hidden" value=""/> <button type="button" class="getbtn" style="outline: none;background: #fff;border: 0;border-left: 1px solid#999;"> 获取验证码 </button> </div> </div>
JS
<script type="text/javascript"> $(".getbtn").on('click', function(){ var tel = $(".getphone").val(); if(!(/^1(3|4|5|7|8)\d{9}$/.test(tel))){ tip("手机号输入有误!请重新输入"); return; } else{ var bb=$("#phone").val(); $.get("__URL__/regist", {iphone: bb}, function (text) { codetime(60); }); /*codetime(60);*/ } }) function codetime(wait){ var _this=$(".getbtn"); if (wait == 0) { _this.attr("disabled", false); _this.html("发送验证码"); _this.css('background-color','#428bca'); }else{ // _this.css('background-color','#ccc'); _this.attr("disabled", true); _this.html("重新发送(" + wait + ")"); wait--; setTimeout(function() { codetime(wait); },1000) } } $(".getsubmit").click(function(){ if(choose()==false){ return; } var bb=$("#yzm").val(); $.get("__URL__/yzm_do", {yzm: bb}, function (text) { if(text=="no"){ tip("验证码输入错误"); return; }else{ $("form").submit(); } }); /* $("form").submit(); */ }) </script>
后台代码
//后台方法 public function yzm_do(){ $yzm=I("yzm"); $yz=$_SESSION['yzm']; if($yzm == $yz){ echo 'yes'; }else{ echo 'no'; } } public function regist(){ $iphone=trim(I("iphone")); //$where['phone']=$iphone; //$fg=M("Adminuser")->where($where)->find(); //$er=count($fg); $yz=getsyz(6);//验证码 $_SESSION['yzm']=$yz; $post_data = array(); $post_data['userid'] = 30; $post_data['account'] = 'ymt';//账号 $post_data['password'] = 'ymt@2018';//密码 $post_data['content'] = '【易码通】你的验证码是'.$yz; //短信内容 $post_data['mobile'] = $iphone; $post_data['sendtime'] = ''; //时定时发送,输入格式YYYY-MM-DD HH:mm:ss的日期值 $url='http://39.108.71.168:8088/sms.aspx?action=send'; $o=''; foreach ($post_data as $k=>$v) { $o.="$k=".urlencode($v).'&'; } $post_data=substr($o,0,-1); $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); //curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //如果需要将结果直接返回到变量里,那加上这句。 $result = curl_exec($ch); echo ""; }