####客户端实现
####保存以下代码运行
<!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="yantel.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>
####服务端实现
####保存以下代码yantel.php给上面调用
<?php
//引入数据库链接配置文件
$con = mysqli_connect('localhost', 'vip', 'vip');
if(! $con )
{
die('连接失败: ' . mysqli_error($con));
}
mysqli_select_db($con,'member');
$tel = "select tel FROM u_users where tel = '$_POST[name]' ";
$result = mysqli_query($con,$tel);
$rows=mysqli_num_rows($result);
if($rows > 0){
echo "手机号已注册" ;
exit;
}else{
echo "手机号可用";
}
mysqli_close($con)
?>