<div class="controls"> <input type="text" id="phone" name="phone" placeholder="请输入你的手机号" class="input-xfat input-xlarge"> <span class="error"></span> </div>
//定义一个方法生成验证码
public function sendcode($phone) { //定义一个随机的四位数 $check = rand(1000,9999); //存储验证码 cache('code',$check); echo $check; $statusStr = array( "0" => "短信发送成功", "-1" => "参数不全", "-2" => "服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!", "30" => "密码错误", "40" => "账号不存在", "41" => "余额不足", "42" => "帐户已过期", "43" => "IP地址限制", "50" => "内容含有敏感词" ); $smsapi = "http://api.smsbao.com/"; $user = "suiyuqi"; //短信平台帐号 $pass = md5("syq112233"); //短信平台密码 $content="$check";//要发送的短信内容 $phone = "$phone";//要发送短信的手机号码 $sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content); $result =file_get_contents($sendurl) ; echo $statusStr[$result]; } public function doregister() { $user = request()->post(); $yzm = cache('code'); if ($user['code'] == $yzm){ unset($user['code'],$user['repassword']); $addRes = Db::name('user')->insert($user); if ($addRes){ return '注册成功'; }else{ return '注册失败'; } }else{ return '验证码错误'; } } <script> $('#dyMobileButton').click(function (){ var phone = $('#phone').val(); $.ajax({ url:'sendcode', type:'get', data:{ phone:phone }, success:function (res){ } }) }) </script>