Web Design Forum: having problems getting to grips with javascript / xml - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

having problems getting to grips with javascript / xml

#1 User is offline   webbhost 

  • Dedicated Member
  • PipPip
  • Group: Members
  • Posts: 192
  • Joined: 12-May 08
  • Reputation: 0
  • Experience:Intermediate
  • Area of Expertise:Designer/Coder

Posted 21 January 2012 - 03:09 PM

Hi all,

Okay so I am trying to get a response from a php page containing XML data, but I cant get it to work... when I alert the length of "users", it returns as 0, but I have 2 users in the XML doc?

Can someone please take a look and tell me what im doing wrong? Ive tried google but its not being very useful...

THE XML PAGE:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<calls>
	<user>
		<callId>2</callid>
		<username>RandomUser</username>
		<answered>1</answered>
		<premiumuser>0</premiumuser>
	</user>
	<user>
		<callId>3</callid>
		<username>RandomUser2</username>
		<answered>0</answered>
		<premiumuser>1</premiumuser>
	</user>
</calls>


The Javascript:

			function LoopConversations(){
				var xmlhttp;
				if (window.XMLHttpRequest)				{// code for IE7+, Firefox, Chrome, Opera, Safari
					xmlhttp=new XMLHttpRequest();
				}else{									// code for IE6, IE5
				  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
				}
				alert ("running, response coming up");
				xmlhttp.onreadystatechange=function(){
					if (xmlhttp.readyState==4 && xmlhttp.status==200){
						var response = xmlhttp.responseXML;
						var users = response.getElementsByTagName("user");
						//***HANDLE THE DISPLAY OF THE TOP BAR HERE. USERS ARE RETURNED AS AN ARRAY.
						alert ("count is "  + users.length);


					}
				}
				xmlhttp.open("GET","LiveAssistConsoleControl.php?LookForCalls=true&refresh=" + (new Date().getTime()),true);
				xmlhttp.send();	

			}

0

#2 User is offline   webbhost 

  • Dedicated Member
  • PipPip
  • Group: Members
  • Posts: 192
  • Joined: 12-May 08
  • Reputation: 0
  • Experience:Intermediate
  • Area of Expertise:Designer/Coder

Posted 21 January 2012 - 03:36 PM

Fixed - Turns out I need to rename the xml returning file as LiveAssistConsoleControl.xml rather than LiveAssistConsoleControl.php...

Oops?
0

#3 User is online   andyl 

  • White space enthusiast
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,535
  • Joined: 21-January 10
  • Reputation: 210
  • Gender:Male
  • Location:Surrey
  • Experience:Intermediate
  • Area of Expertise:Web Developer

Posted 21 January 2012 - 03:52 PM

Stop. Use jQuery and JSON to save yourself a packet of Nurofen.

Your PHP script should return (print/echo/die) like so:

<?

$response = array(
	'calls' => array(
                //The '2' and '3' are callid variables - unique
		2 => array(
			'username' => 'RandomUser',
			'answered' => 1,
			'premiumuser' => 0
		),
		3 => array(
			'username' => 'RandomUser',
			'answered' => 0,
			'premiumuser' => 1
		)
	)
);

print json_encode($response);

?>


And your jQuery:

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">

$(document).ready(function(){

	//Handles the response
	//Response looks like:
	//	{"calls":{"2":{"username":"RandomUser","answered":1,"premiumuser":0},"3":{"username":"RandomUser","answered":0,"premiumuser":1}}}
	var jsonresponsehandler = function(obj){
		try {
			$.each(obj.calls, function(id, obj){
				alert("CallID: " + id + 
					  "\nUsername: " + obj.username + 
					  "\nAnswered: " + obj.answered + 
					  "\nPremium: " + obj.premiumuser);
			});
		} catch (e) {
			alert("Invalid response from script");
		}
	};

	//Make the call to your script (change json.php to your PHP script)
	//Adding date var prevents unwanted caching
	$.getJSON('json.php?' + (new Date()).getTime(), jsonresponsehandler);

});

</script>




Edit: After seeing your later post, I'm assuming your XML file is static. If so, carry on as you were and ignore me ;)

This post has been edited by andyl: 21 January 2012 - 03:53 PM

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users