Daniele Cruciani
Programmatore Developer PHP/MySQL Freelance
mobile: +39 3489215204

icq skype msn linkedin fb t ff youtube picasa google
seconds to the end of World
End of World 21 December, 2012 11:11:00
Hire me before that day! | don't bother

You are here

XMLHttpRequest, document element DOM parsing

    XMLHttpRequest use, works only if url is on same site. checklogin.php give <KO/> if login è fails <OK>XXX</OK> with XXX id of user when login è is ok

    var loginOK = false;
    
    var httpRequest;
    
    function validateLogin()
    {
     var login = document.getElementById("NomeForm");
     var logintxt = login.value;
     var password = document.getElementById("PasswdForm");
     var passwordtxt = password.value;
     var url = "/forum/checklogin.php?username="
     + logintxt + "&password=" + passwordtxt;
    
     if (window.XMLHttpRequest) { // Mozilla, Safari, ...
        httpRequest = new XMLHttpRequest();
        if (httpRequest.overrideMimeType) {
           httpRequest.overrideMimeType('text/xml');
        }
     }
     else if (window.ActiveXObject) { // IE
        try {
           httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
           try {
              httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
           }
           catch (e) {}
        }
     }
    
     if (!httpRequest) {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
     }
     httpRequest.onreadystatechange = loadLogin;
     httpRequest.open('GET', url, true);
     httpRequest.send(null);
    }
    
    function loadLogin()
    {
      if (httpRequest.readyState == 4) {
          if (httpRequest.status == 200) {
             response = httpRequest.responseText;
    	 if (response == "") {
    	   alert ('login/password errate.');
    	 } else {
    	   var responseXML = httpRequest.responseXML.documentElement;
    	   user_id = responseXML.firstChild.nodeValue;
    	   LinkSitoForm = document.getElementById('LinkSitoForm');
    	   LinkSitoForm.setAttribute('value', 'http://forum.cellularmagazine.it/profile.php?mode=viewprofile&u=' + user_id);
    	   NomeForm = document.getElementById('NomeForm');
    	   PasswdForm = document.getElementById('PasswdForm');
    	   PasswdForm.setAttribute('enabled','no');
    	   var whchis = document.getElementById("trcheckpasswd");
    	   whchis.innerHTML = "OK"
    	 }
          } else {
             alert('There was a problem with the request.');
          }
       }
       else {
       }
    
    }
    
    Undefined