Ajax模拟登陆验证
index.php
<script language="javascript">
var http_request = false;
function createRequest(url){
http_request = false;
if(window.XMLHttpRequest){ //Mozilla、Safari等浏览器
http_request = new XMLHttpRequest();
}else if(window.ActiveXObject){ //IE
try{
http_request = new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){}
}
}
if(!http_request){
alert("不能创建XMLHTTP实例!");
return false;
}
http_request.onreadystatechange = alertContents; //指定相应方法
//发出HTTP请求
http_request.open("GET" ,url ,true);
http_request.send(null);
}
function alertContents() {
if (http_request.readyState == 4) {//处理服务器返回的信息
if (http_request.status == 200) {
alert(http_request.responseText);
} else {
alert("Ajax验证页面发生错误");
}
}
}
</script>
<script language="javascript">
function checkName(){
var username = form1.name.value;
if(username == ""){
window.alert("name is null!");
return false;
}else {
createRequest('checkname.php?username=' + username + '&nocache=' + new Date().getTime());
}
}
</script>
<form name="form1" method="post" action="">
<select name="name">
<option value="xiaoming">xiaoming</option>
<option value="xiaoli">xiaoli</option>
<option value="xiaowang">xiaowang</option>
</select>
<input type="submit" name="Submit" value="Ajax" οnclick="checkName()">
</form>
CheckName.php
<?php
//模拟服务器验证
$username = $_GET['username'];
echo 'Ajax Check web Get Name: '.$username;
?>