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 ..
freshgraduate09 viết 12:00 ngày 10/10/2018
là gì? http://vi.wikipedia.org/wiki/Ajax_(l...ADp_tr%C3%ACnh)

tutorial: http://www.w3schools.com/Ajax/Default.Asp

thư viện hỗ trợ http://jquery.com/
bachnga viết 12:12 ngày 10/10/2018
Bạn hãy thử một ví dụ nhỏ sau đây, chứ đọc cả đống thì ... để từ từ vậy.
1. Form đăng ký (trong file register.asp chẳng hạn)
Code:
<script language="javascript" type="text/javascript" src="ajax.js"></script>
<form>
Username: <input type="text" size="20" maxlength="20" onblur="goTest(this)" />
<label id="status" style="font-weight:800">&nbsp;</label><br />
Email: <input type="text" size="20" maxlength="50" name="email" />
</form>
2. File javascript: ajax.js
PHP Code:
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;
        }
    }

3. File checkuser.asp
Code:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
Session.CodePage=65001
Dim username, list_users, userstatus
'Giả sử danh sách username đã có
list_users = ",nam,hung,minh,lan,dung,mai,tam,"
username = Request.Form("username")
If InStr(1,list_users,"," & LCase(username & ","),1)>=1 Then
	Response.Write("0")
Else
	Response.Write("1")
End If
%>
Đặt 3 file này chung thư mục, như trong root của IIS chẳng hạn.
Mở trình duyệt và gõ: http://localhost/register.asp
1n1 viết 12:08 ngày 10/10/2018
bạn bachnga kì công quá
kenphan19 viết 11:58 ngày 10/10/2018
dùng cái này Dùng jQuery Ajax để checking username forms
DEMO ở đây và DOWNLOAD
mobell200 viết 12:00 ngày 10/10/2018
function ajax(){ var ajaxrequest; ajaxrequest=new XMLHttpRequest();
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
ndp1007 viết 12:12 ngày 10/10/2018
Cảm ơn các bạn đã nhiệt tình chia sẽ...!
Chúc các bạn thành công!
Bài liên quan
0