Muddy_Funster Posted September 8, 2011 Share Posted September 8, 2011 OK, I've been encouraged to start and include Ajax in some new site development. Problem is, I've never touched it before so I suck at it, so silly little problems like this bugger me up. I'm trying to get a login form to validate and return xml output that is then displayed in div by the ajax function. I get the "http.responseXML is null" error when I try and do this. I have pretty much copy/paste and then edited code snippets off the net to get this far, and I'm starting to get my head arround what does what and where it does it, but I can't find what the problem is with this. the script code is: // JavaScript AJAX Document var http = createRequestObject(); function createRequestObject() { // find the correct xmlHTTP, works with IE, FF and Opera var xmlhttp; try { xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { try { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) { xmlhttp=null; } } if(!xmlhttp&&typeof XMLHttpRequest!="undefined") { xmlhttp=new XMLHttpRequest(); } return xmlhttp; } function GetID(uName, uPass){ var name = uName; var pass = uPass; if(name.length >0 && pass.length >0) { name=escape(name); pass=escape(pass); try{ http.open("GET", "inc/login.php?name="+name+"&password="+pass, true); http.setRequestHeader('Content-Type', "text/xml"); http.onreadystatechange = handleResponse; http.send(null); } catch(e){ // caught an error alert('Request send failed.'); } finally{} } else { alert("please complete both fields"); } } function handleResponse() { try{ if((http.readyState < 4)||(http.status != 200)){ var ajaxDisplay = document.getElementById('main'); ajaxDisplay.innerHTML = '<BR><BR><center><img src="./img/loading6.gif" alt="Loading..."><BR>Loading...</center>'; } if((http.readyState == 4)&&(http.status == 200)){ var response = http.responseXML.documentElement; var n = response.getElementsByTagName('message')[0].firstChild.nodeValue; var e = response.getElementsByTagName('status')[0].firstChild.nodeValue; var r = response.getElementsByTagName('contnent')[0].firstChild.nodeValue; // write out response document.getElementById("main").innerHTML = r; alert(n); } } catch(e){ // caught an error alert('Response failed. >>'+e); } finally{} } the login.php file (still commented from when I was checking it in browser) <?php /* @SESSION_START(); require_once './connection.php'; $name = mysql_real_escape_string($_GET['name']); $pass = mysql_real_escape_string($_GET['password']); $logSQL = "SELECT branchNo FROM branch WHERE authLogin ='$name' AND branchPass = '$pass'"; $logCHK = mysql_query($logSQL); if(mysql_num_rows($logCHK) <> 1 ){ */ print "<?xml version='1.0' encoding='ISO-8859-1'?> <xml> <status>F</status> <message>Either your login ID or password are incorect, please try again</message> <content><?php require_once 'first_check.php'; ?></content> </xml>"; /* } else{ $logRES = mysql_fetch_array($logCHK); echo "Login Successfull"; $_SESSION['id'] = $logRES['branchNo']; print "<?xml version='1.0' encoding='ISO-8859-1'?> <xml> <status>Y</status> <message>Loggin Succsessful</message> <content><?php require_once 'first_check.php'; ?></content> </xml>"; }*/ ?> and the page with the login form: <?php if (!isset($_SESSION['id'])){ ?> <div id="login"> <form action="#" name ="login" method="post"> <center>Login:<br /> Login ID: <input type="text" name="name" id="name" value=""><br><br> Password: <input type="password" name="password" id="password" value=""><br> <input type="button" alt="Login" value="Login" onclick="GetID(login.name.value, login.password.value)"> </center> </form> </div> </p>Welcome to the new McConechy's Intranet Site. Updates and construction are still ongoing at the moment, with only some of the site operational.</p> <p> To Access most feetures you are requred to log in. this is both to maintain security, and also to identify you on the site.</p> <?php } else{ print " You are Logged In. Updates and construction are still ongoing at the moment, with only some of the site operational. Please use the buttons on the left to navigate. "; } ?> I have checked the generated XML and it's fine as far as any validators are concerned, I also checked the link in firebug when the error comes up and it contains the xml as it should be, with nothing additional/erronious that I can see. I have the type set to text/xml (as you can see in the code) and have checked the login.php directly in the browser and it is producing the data as it should. This is still very early development so it's a good bit messy, that will be deat with in due course, but I sure would appreciate some assistance on this one. Quote Link to comment https://forums.phpfreaks.com/topic/246699-httpresponsexml-is-null/ Share on other sites More sharing options...
PFMaBiSmAd Posted September 8, 2011 Share Posted September 8, 2011 Not sure if this is the problem, but because it is a .php file that is outputting an xml document, add the following header statement - header("Content-type: text/xml"); Quote Link to comment https://forums.phpfreaks.com/topic/246699-httpresponsexml-is-null/#findComment-1266915 Share on other sites More sharing options...
Muddy_Funster Posted September 8, 2011 Author Share Posted September 8, 2011 Genius! see, if I knew what I was dooing I wouldn't look quite as stupid as I feel! That was exactly the issue and now the script is working. On to figure out how to populate the div now. Thanks again PFMaBiSmAd Quote Link to comment https://forums.phpfreaks.com/topic/246699-httpresponsexml-is-null/#findComment-1266943 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.