This article mainly shares with you an example of ajax+php implementing non-refresh verification of mobile phone numbers. It has a good reference value and I hope it will be helpful to everyone. Let’s follow the editor to take a look, I hope it can help everyone.
A simple non-refresh verification method to verify whether a mobile phone number is registered:

Client implementation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | <!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>
|
Copy after login
Server implementation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?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 )
?>
|
Copy after login
Related recommendations:
asp.net ajax to achieve no-refresh verification code
php+jquery ajax email address no-refresh verification Example_PHP tutorial
jquery implements a simple example of non-refresh verification code_jquery
The above is the detailed content of Example analysis of ajax and php to achieve non-refresh verification of mobile phone number. For more information, please follow other related articles on the PHP Chinese website!