Home  >  Article  >  Backend Development  >  Example explanation of ajax+php to implement non-refresh verification of mobile phone number

Example explanation of ajax+php to implement non-refresh verification of mobile phone number

jacklove
jackloveOriginal
2018-07-04 18:02:361441browse

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(&#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)
?>

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.

Articles you may be interested in:

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!

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