The editor below will share with you an example of using ajax php to verify mobile phone numbers without refreshing. It has a good reference value and I hope it will be helpful to everyone. Let’s follow the editor and take a look.
A simple no-refresh verification method to verify whether a mobile phone number is registered:
Client implementation:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style> #btn{width:80px;height:30px;} </style> </head> <body> <form> 请输入手机号: <input type="text" id="name" name="name"> <input type="button" onclick="show()" id="btn" value="验证"> </form> <p id="txt"></p> <script> var usernamereg = /^1(3|4|5|7|8)\d{9}$/; function show(){ var name=document.getElementById("name").value; var xmlHttp=new XMLHttpRequest(); var url="post.php"; xmlHttp.onreadystatechange=function stateChanged() { if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ if(!usernamereg.test(name)){ document.getElementById("txt").innerHTML="手机号有误"; }else{ document.getElementById("txt").innerHTML=xmlHttp.responseText } } } xmlHttp.open("POST",url,true); xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xmlHttp.send("name=" + name); } </script> </body> </html>
Server implementation:
<?php $con = mysqli_connect('localhost', 'username', 'password'); if(! $con ) { die('连接失败: ' . mysqli_error($con)); } mysqli_select_db($con,'bdm256727651_db'); $username = "select userName FROM Forms where userName = '$_POST[name]' "; $result = mysqli_query($con,$username ); $rows=mysqli_num_rows($result); if($rows){ echo "手机号已注册" ; exit; }else{ echo "手机号可用"; } mysqli_close($con) ?>
The above example of ajax php implementing non-refresh verification of mobile phone number is all the content shared by the editor. I hope it can help It is a reference for everyone, and I hope everyone will support the php Chinese website.
Explanation of a simple implementation method for PHP to read and output XML file data
thinkphp5 Explanation of the method of loading static resource paths and constants
A brief analysis of closures and anonymous functions in PHP
The above is the detailed content of Example explanation of ajax+php to implement non-refresh verification of mobile phone number. For more information, please follow other related articles on the PHP Chinese website!