10/10/2018, 09:56
[HELP]Ajax-Kiểm tra username khi đăng ký?
Mình có bài tập web(Viết bằng ASP) kiểm tra username khi đăng ký
Source : Download
Xử lý trường hợp sau:
1.Khi Username hợp lệ(chưa có trong CSDL)

2.Khi Username không hợp lệ(Đã tồn tại trong CSDL)

Nhưng mình muốn kiểm tra ngay tức thì(Khi Focus vừa chuyển qua 1 Input text khác):
Hình mô phỏng như sau:

Mình nghe nói Ajax làm được ,nhưng chưa biết Ajax là gì.
Bài tập của mình nhúng Ajax vào như thế nào.Mong các bạn chia sẽ và hướng dẫn.
Cảm ơn các bạn nhiều ..
Source : Download
Xử lý trường hợp sau:
1.Khi Username hợp lệ(chưa có trong CSDL)

2.Khi Username không hợp lệ(Đã tồn tại trong CSDL)

Nhưng mình muốn kiểm tra ngay tức thì(Khi Focus vừa chuyển qua 1 Input text khác):
Hình mô phỏng như sau:

Mình nghe nói Ajax làm được ,nhưng chưa biết Ajax là gì.
Bài tập của mình nhúng Ajax vào như thế nào.Mong các bạn chia sẽ và hướng dẫn.
Cảm ơn các bạn nhiều ..
Bài liên quan
tutorial: http://www.w3schools.com/Ajax/Default.Asp
thư viện hỗ trợ http://jquery.com/
1. Form đăng ký (trong file register.asp chẳng hạn)
var req;
var tagid;
var previous_value = "";
var current_value = "";
function goTest(obj){
current_value = obj.value;
tagid = "status";
if(current_value!="" && current_value != previous_value){
var url = "checkuser.asp";
var pars = "username=" + current_value;
retrieveURL(url,pars);
}
}
function retrieveURL(url,pars) {
if (window.XMLHttpRequest) { // Non-IE browsers
req = new XMLHttpRequest();
req.onreadystatechange = processStateChange;
try {
req.open("POST", url);
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
req.setRequestHeader("charset", "utf-8");
req.send(pars);
} catch (e) {
alert(e);
}
//req.send(null);
} else if (window.ActiveXObject) { // IE
req = new ActiveXObject("Microsoft.XMLHTTP");
if (req) {
req.onreadystatechange = processStateChange;
req.open("POST", url);
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
req.setRequestHeader("charset", "utf-8");
req.send(pars);
}
}
}
function processStateChange() {
var id = document.getElementById(tagid);
if (req.readyState == 4) { // Complete
if (req.status == 200) { // OK response
if(req.responseText=="1"){
id.innerHTML = "Chấp nhận.";
id.style.color = "blue";
}else{
id.innerHTML = "Không chấp nhận.";
id.style.color = "red";
}
previous_value = current_value;
}
}
}
Mở trình duyệt và gõ: http://localhost/register.asp
DEMO ở đây và DOWNLOAD
ajaxrequest.onreadystatechange=function() { if(ajaxrequest.readyState==4)
{ document.myform.kq.value=ajaxrequest.responseText; } }
var so1=document.getElementById('so1').value;
var so2=document.getElementById('so2').value;
ajaxrequest.open("Get","ketqua.php?so1="+so1+"&so2 ="+so2,true);
ajaxrequest.send(null); }
</script>
<form name="myform">
<input type="text" id="so1" />
<input type="text" id="so2" />
<input type="text" id="kq" />
<input type="button" value="ok" onclick="ajax()" />
</form>
ketqua.php
$so1=$_GET['so1'];
$so2=$_GET['so2'];
$kq=$so1+$so2;
echo($kq);
cái nè còn đơn giản hơn
1 vài ví dụ đơn giản về php
http://my.opera.com/shimiphp/blog/index.dml/tag/ajax
Chúc các bạn thành công!