Jump to content

Parse Xml With Js?


hybrid kill3r
 Share

Recommended Posts

I am using PHP to set XML data that I am grabbing with my JavaScript code to get a response from the server about form data(aka form validation). If the user's login name isn't found in the database, it sets an error in xml saying so. If the login name was found but the password didn't match, it also sets an error in xml saying so. How do I read certain xml tags with javascript? I know my XML is being outputted correctly because I visit the PHP with the information already in the URL. I load the XML with ajax.Example XML:

<login>−<error field="loginName"><message>Yes!</message> //This is the main tag that I want to get.  I will return the form true/false based on the content of this tag with JS.<return>true</return></error>−<error field="password"><message>Incorrect password!</message> //This is the main tag that I want to get.  I will return the form true/false based on the content of this tag with JS.<return>false</return></error></login>

java script:

var xmlhttp;function verifyInfo(){		var loginName = document.getElementById("loginName").value;	var password = document.getElementById("password").value;	loadAjax("javascript/ajax/login.php?act=verify&loginname="+loginName+"&password="+password);		if(xmlhttp.readyState == 4){		nameMessage = xmlDoc.documentElement.childNodes[0].firstChild.textContent;		passMessage = xmlDoc.documentElement.childNodes[1].firstChild.textContent;		alert(nameMessage);		if(nameMessage == "No such login name!"){			alert(nameMessage);			return false;		} else if(passMessage == "Incorrect password!"){			alert(passMessage);			return false;		} else {			return true;		}	}}		function loadAjax(url){				xmlhttp=GetXmlHttpObject();		if (xmlhttp==null){			  alert ("Your browser does not support AJAX!"); 			 return;		}		xmlhttp.open("GET",url,true);		xmlhttp.send(null);			}

Edited by Hybrid Kill3r
Link to comment
Share on other sites

It looks like you've misunderstood how AJAX (or rather - the XMLHttpRequest() object) operates.Once you initialize the object, and before sending the request, you must assign a function that will be called each time the readyState changes (e.g. when the file is loaded - 4) to the onreadystatecange proprty. At this function, you can then extract the response from the responseXML property of XMLHttpRequest.I'd suggest the following (completely replacing what you had):

function verifyInfo(){		var loginName = document.getElementById("loginName").value;	var password = document.getElementById("password").value;	var xmlhttp = loadAjax("javascript/ajax/login.php?act=verify&loginname="+loginName+"&password="+password);	xmlhttp.onreadystatechange = function() {		if(xmlhttp.readyState == 4){			var xmlDoc = xmlhttp.responseXML;			var nameMessage = xmlDoc.documentElement.childNodes[0].firstChild.textContent;			var passMessage = xmlDoc.documentElement.childNodes[1].firstChild.textContent;			//alert(nameMessage);			if(nameMessage == "No such login name!"){				alert(nameMessage);				return false;			} else if(passMessage == "Incorrect password!"){				alert(passMessage);				return false;			} else {				return true;			}		}	}	xmlhttp.send(null);}		function loadAjax(url){				var xmlhttp=GetXmlHttpObject();		if (xmlhttp==null){			  alert ("Your browser does not support AJAX!");			 return;		}		xmlhttp.open("GET",url,true);		return xmlhttp;	}

Edited by boen_robot
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...