Ajax:项目实例-表单登陆

1、使用Ajax实现以下 功能-用户表单登陆:
1)、用户表单登陆时判断是否为注册用户
2)、若是新用户提示,先进行注册,禁用登陆按钮
3)、若是老用户提示“登陆中,”
HTML代码:

User login

JS代码:

addEvent(window,‘load’,function(){
var user=document.getElementById(‘name’);
//Cteate focus event
addEvent(user,‘blur’,function(){
//Create an Ajax Object
var xhr = createXHR();
//Create event listener
xhr.onreadystatechange = function(){
if(xhr.readyState==4 && xhr.status == 200){
if(xhr.responseText == 0)
document.getElementById(‘warning’).innerHTML=‘New user!’
document.getElementById(‘submit’).disabled=true;
}
}
};
//Create url Request
var user=document.getElementById(‘name’).value;
var password = document.getElementById(‘password’).value;
var date = ‘name=’+user+’&password=’+password;
var url =‘chek.php’;
xhr.open(‘post’,url,true);
// Set header information
xhr.setRequestHeader(‘content-type’,‘application/x-www-form-urlencoded’);
//Send Request
xhr.send(date);
//Create event listener
var submit= document.getElementById(‘submit’);
addEvent(submit,‘click’,function(evt){
document.getElementById(‘tips’).innerHTML=‘pass’;
//return false;
preDef(evt);
});

});

})

PHP代码

你可能感兴趣的:(Ajax:项目实例-表单登陆)