<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>新建网页</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<script type="text/javascript">
function checkname(){
//ajax校验用户名
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if(xhr.readyState==4){
document.getElementById('result').innerHTML = xhr.responseText;
}
}
//获得输入的用户名,并传递个服务器端
var ming = document.getElementById('username').value;
//对ming变量的特殊符号信息进行编码
ming = encodeURIComponent(ming); //JavaScript中进行URL编码。
xhr.open('get','./04.php?nm='+ming+'&age=23'); //在url中传递get参数
xhr.send(null); //get请求,send参数是null
}
</script>
</head>
<body>
<h2>ajax之get形式请求</h2>
<p>用户名:<input type="text" id="username" οnblur="checkname()" /></p>
<p>密码:<input type="password" id="userpwd" /></p>
<div id="result"></div>
</body>
</html>
04.php:
<?php
//服务器端校验用户名
$exists = array('linken','mary','jim');
print_r($_GET);
exit;
$name = $_GET['nm'];
//判断用户名是否存在
if(in_array($name,$exists)){
echo "<span style='color:red'>用户名已经存在,请换一个</span>";
}else{
echo "<span style='color:green'>恭喜,可以使用此名字</span>";
}