日期:2014-05-16 浏览次数:20679 次
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>校验用户名</title> <script type="text/javascript"> //创建ajax引擎 function getXmlHttpObject(){ var xmlHttpRequest; //不同浏览器获取对象是不一样的 if (window.ActiveXObject){ //获取ie浏览器中的XmlHttpRequest对象 xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP"); }else{ //获取其他浏览器中的XmlHttpRequest对象 xmlHttpRequest = new XMLHttpRequest(); } return xmlHttpRequest; } var xmlHttpRequest; function checkName(){ //获取XmlHttpRequest对象 xmlHttpRequest = getXmlHttpObject(); //指定请求的url var url = "/EPP/ajax/checkname.php"; xmlHttpRequest.open("post",url,true);//true为是否异步加载 xmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); //注册回调函数 xmlHttpRequest.onreadystatechange = callBack; //需要向服务器发送的数据 var data = "username=" +$("username").value; xmlHttpRequest.send(data);//发送数据 } function callBack(){ if (xmlHttpRequest.readyState == 4){ //将接收的数据显示到页面上 $('span').innerText = xmlHttpRequest.responseText; } } function $(id){ return document.getElementById(id); } </script> </head> <body> <form action="checkname.php" method="post"> 用户名:<input type="text" id="username" name="username"/> <input type="button" onclick="checkName()" value="校验"/><span id="span" style="color : red;"></span> </form> </body> </html>
<?php $username = $_POST['username']; if ($username != ""){ if ($username == "fenghuo"){ echo "用户名不可用"; }else{ echo "用户名可用"; } }else{ echo "请输入用户名"; } ?>