This article is mainly for everyone to introduce the AJAX submitted mobile phone number to database verification and return the status of the relevant data, with a certain reference value, interested in small partners can refer to, hope to help everyone.
<script type= "Text/javascript" > $ (Function () {$ ('. Agree_regi '). Click (function () { var phone = $.trim ($ ("#") Phone "). Val ()); if (phone = = "") { Newalert (2, "Please enter phone number", null); return false; } else{ var reg =/^0?1[3|4|5|8| 7][0-9]\d{8}$/; if (!reg.test (phone)) { Newalert (2, "Please enter a valid mobile number", null); return false; } } var data ={ Phone:phone, }; $.ajax ({ type: "POST", URL: "{: U (' Register/phonefind ')}", Data:data, success:function (msg) { if (msg== ' 0 ') { Newalert (2, "phone number is wrong", null); } if (msg== ' 1 ') { Newalert (2, "the mobile number is already registered, please login directly", null); } if (msg== ' 2 ') { location.href= "/register/regowner?phone=" +phone; } if (msg== ' 3 ') { location.href= "/register/regnest?phone=" +phone;}});} ); });</script>
The backend receives the Ajax commit value, goes to the database query, and returns.
public function Phonefind () {if (!empty (I (' Param.phone '))) {//i method gets the value of the post commit $phone = I (' param . Phone '); $user = M ("Cuser"); $res = $user->where (Array (' phone ' = $phone))->find (); Go to the database query one, and return the IF (!empty ($res [' password ')) {$status =1;//password exists, the user directly login}elseif (!empty ($res)) {$status =2;//exists, No password, set password, is the owner of}else{$status =3;//does not exist, is a visitor, registration}}else{$status =0;//mobile phone number is wrong} $this->ajaxreturn ($status); Returns the status value to the foreground}