Phil Bannon
Phil Bannon

Reputation: 11

using math with XMLHttpRequest data

I have been asked if I can use some form data and present it in HTML.

I need to add up the LOADS data/values and multiply it by the RATE data/values, I thought this would have worked but its not showing anything in my browser?

Where am I going wrong?

I'm new to JavaScript and I have HTML/CSS skills. I know jQuery is probably the best way to do this sort of stuff but I don't know it.

<html>
<script>
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET","xmltest.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML; 

document.write("<table border='1'>");
var k=xmlDoc.x[1].getElementsByTagName("LOADS");
var J=xmlDoc.x[1].getElementsByTagName("RATE");
  { 
  document.write("<tr ><td>");
  document.write(k*J);
  document.write("</td><tr>");
  }

document.write("</table>");

</script>
</html>



XML file

<MULTILOAD_TICKET>
<TICKET>
<DATE>12/11/12</DATE>
<ADDRESS>123 FAKE STREET</ADDRESS>
<RATE>300</RATE>
<LOADS>3</LOADS>
<CUSTOMER>Columbia Ales</CUSTOMER>
<ORDERID>BBKHJ1001</ORDERID>
<DRIVER>BOB</DRIVER>
<VEHICAL_REG>UJ78 JHE</VEHICAL_REG>
<MATERIAL>SPOIL</MATERIAL>
<SIG>URL</SIG>
</TICKET>

<TICKET>
<DATE>12/11/12</DATE>
<ADDRESS>123 FAKE STREET</ADDRESS>
<RATE>300</RATE>
<LOADS>6</LOADS>
<CUSTOMER>Columbia Ales</CUSTOMER>
<ORDERID>BBKHJ1001</ORDERID>
<DRIVER>JACK</DRIVER>
<VEHICAL_REG>EU78 JHD</VEHICAL_REG>
<MATERIAL>SPOIL</MATERIAL>
<SIG>URL</SIG>
</TICKET>

<TICKET>
<DATE>15/11/12</DATE>
<ADDRESS>123 FAKE STREET</ADDRESS>
<RATE>300</RATE>
<LOADS>5</LOADS>
<CUSTOMER>Columbia Ales</CUSTOMER>
<ORDERID>BBKHJ1001</ORDERID>
<DRIVER>BOB</DRIVER>
<VEHICAL_REG>UJ78 JHE</VEHICAL_REG>
<MATERIAL>SPOIL</MATERIAL>
<SIG>URL</SIG>
</TICKET>

</MULTILOAD_TICKET>

Upvotes: 1

Views: 142

Answers (1)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324630

k and J refer to a collection of elements, so you can't just multiply them together.

You need to loop through the collections, access their textContent (or innerText depending on browser version), convert to numbers (parseInt or parseFloat as needed), multiply them and then add them to a running total. Finally, at the end, you can output the total.

Upvotes: 1

Related Questions