Home >Backend Development >PHP Tutorial >Example analysis of ajax and php to achieve non-refresh verification of mobile phone number

Example analysis of ajax and php to achieve non-refresh verification of mobile phone number

小云云
小云云Original
2017-12-23 13:38:351585browse

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:

<!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(&#39;localhost&#39;, &#39;username&#39;, &#39;password&#39;);
if(! $con )
{
 die(&#39;连接失败: &#39; . mysqli_error($con));
}
mysqli_select_db($con,&#39;bdm256727651_db&#39;);
$username = "select userName FROM Forms where userName = &#39;$_POST[name]&#39; ";
$result = mysqli_query($con,$username );
$rows=mysqli_num_rows($result);
if($rows){
	echo "手机号已注册"	;
	exit;
}else{
	echo "手机号可用";	
}
mysqli_close($con)
?>

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn