By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,528 Members | 1,375 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,528 IT Pros & Developers. It's quick & easy.

adding a value

P: 47
i've put this code together.

there's a variable "s" giving me some grief at the mo, i'm actualy supprised it half works.

it's a simple shopping cart, you pick the quantitly of items, and it adds the total quantitly, total price including vat, that worked, now, that i've introduced shipping, i'm having a bit of trouble.

the shipping value is held in the var s, which i thought i could just add on to the total ammount........

now, it works if you choose shipping first, and then choose quantitly of items, but if you select some items first, you get a NaN in the total box.

here's the code, hope it makes more sence, thanks.


Expand|Select|Wrap|Line Numbers
  1.  
  2. <script language="javascript1.2">
  3.  
  4.  
  5. function checkreset()
  6. {
  7.  
  8.    if (confirm('Are you sure you wish to clear the form?'))
  9.    {
  10.       return true
  11.    }
  12.    return false
  13. }
  14.  
  15. function addnumbers()
  16. {
  17.  
  18. // This function reads the values from the input boxes
  19. // parseInt converts the input boxes into an integer
  20.  
  21. //loops through the shipping radio boxes
  22.  
  23.     var s, i;
  24.  
  25.     for (i=0; i<document.form1.shipping.length;i++)
  26.     {
  27.         if (document.form1.shipping[i].checked)
  28.         {
  29.             s = parseInt(document.form1.shipping[i].value);
  30.         }
  31.     }
  32.  
  33.  
  34. a = parseInt(document.form1.abox.value);
  35. b = parseInt(document.form1.bbox.value);
  36. c = parseInt(document.form1.cbox.value);
  37. d = parseInt(document.form1.dbox.value);
  38. e = parseInt(document.form1.ebox.value);
  39. f = parseInt(document.form1.fbox.value);
  40. g = parseInt(document.form1.gbox.value);
  41. h = parseInt(document.form1.hbox.value);
  42. i = parseInt(document.form1.ibox.value);
  43. j = parseInt(document.form1.jbox.value);
  44.  
  45.  
  46.  
  47. // Then we do the math
  48.  
  49. result = a + b + c + d + e + f + g + h + i + j;
  50.  
  51. // This adds the total number of products to the quantity box
  52. document.form1.qbox.value = result;
  53.  
  54. // This calculates the total price, including vat at 21% and adds the shipping cost
  55. // toFixed fixes the decimal point 2 places
  56. document.form1.totalbox.value = "Ä" + (s+result*10*21/100+result*10).toFixed(2);
  57.  
  58.  
  59. // As the function has already done the output, we donít need a return value
  60. }
  61. </script>
  62.  
  63.  
  64.  

and the html


Expand|Select|Wrap|Line Numbers
  1.  
  2. <form name="form1" onReset="return checkreset()">
  3.  
  4. <!--#include virtual="inc/top.inc" --> 
  5.  
  6.  
  7. <table width="700" border="0" cellpadding="15" cellspacing="1" bgcolor="#FF6600">
  8.   <tr>
  9.     <td width="65%" bgcolor="#FFFFFF"><img src="images/t1.jpg" alt="never forget" width="100" height="75" /> <span class="style1">&nbsp;<span class="style2">                  Never Forget: <span class="style4">&euro;10</span> </span><br />
  10.       <br />
  11.       Quantity</span> 
  12.     <input type="text" id="abox" onChange="addnumbers()" value="0" size="3" maxlength="3">
  13.     &nbsp;<span class="style1">Gift Wrap? </span>
  14.     <label>
  15.     <input name="gift" type="checkbox" id="gift" value="checkbox" />
  16.     <br />
  17.     </label></td>
  18.     <td width="35%" rowspan="10" valign="top" bgcolor="#FFFFFF">
  19.  
  20.  
  21.  
  22.     <table width="100%" border="0" cellspacing="1" cellpadding="6">
  23.       <tr>
  24.         <td colspan="3" bgcolor="#FFF7D2"><span class="style3">Please choose your region for shipping </span></td>
  25.         </tr>
  26.       <tr>
  27.         <td align="center"><span class="style1"><strong>Ireland<br />
  28.         </strong>free </span><br />
  29.          <input type="radio" name="shipping" onClick="addnumbers()" value="0"></td>
  30.         <td align="center"><span class="style1"><strong>Other EU</strong><br />
  31.           &euro;5<br />
  32. </span>
  33.         #  <input type="radio" name="shipping" onClick="addnumbers()" value="5">
  34.         #</td>
  35.         <td align="center">&nbsp; <span class="style1"><strong>Rest of World</strong><br />
  36.           &euro;10<br />
  37. </span>
  38.           <input type="radio" name="shipping" onClick="addnumbers()" value="10"></td>
  39.       </tr>
  40.     </table>
  41.  
  42.       <table width="100%" border="0" cellspacing="1" cellpadding="6">
  43.         <tr>
  44.           <td colspan="2" bgcolor="#FFF7D2"><span class="style3">Shopping Cart </span></td>
  45.         </tr>
  46.         <tr>
  47.           <td><span class="style1">Quantity</span></td>
  48.           <td><input type="text" id="qbox" value="0"></td>
  49.         </tr>
  50.         <tr>
  51.           <td><span class="style1">Total</span></td>
  52.           <td><input type="text" id="totalbox" value="0"></td>
  53.         </tr>
  54.         <tr>
  55.           <td></td>
  56.           <td align="right"><input type="reset" value="Reset"></td>
  57.         </tr>
  58.       </table> 
  59.  
  60.  
  61.     <br /></td>
  62.   </tr>
  63.   <tr>
  64.     <td bgcolor="#FFFFFF"><img src="images/t2.jpg" alt="never forget" width="100" height="75" /> <span class="style1">&nbsp;<span class="style2">                   Viva La Evoluci&oacute;n: <span class="style4">&euro;10</span></span></span><span class="style1"><br />
  65.       <br />
  66.       Quantity</span>
  67.        <input type="text" id="bbox" onChange="addnumbers()" value="0" size="3" maxlength="3">
  68. &nbsp;<span class="style1">Gift Wrap? </span>
  69. <label>
  70. <input name="gift2" type="checkbox" id="gift2" value="checkbox" />
  71. </label></td>
  72.   </tr>
  73.   <tr>
  74.     <td bgcolor="#FFFFFF"><img src="images/t3.jpg" alt="never forget" width="100" height="75" /> <span class="style1">&nbsp;<span class="style2"> Finders Keepers: <span class="style4">&euro;10</span></span></span><span class="style1"><br />
  75.       <br />
  76.       Quantity</span>
  77.        <input type="text" id="cbox" onChange="addnumbers()" value="0" size="3" maxlength="3">
  78. &nbsp;<span class="style1">Gift Wrap? </span>
  79. <label>
  80. <input name="gift22" type="checkbox" id="gift22" value="checkbox" />
  81. </label></td>
  82.   </tr>
  83.   <tr>
  84.     <td bgcolor="#FFFFFF"><img src="images/t4.jpg" alt="never forget" width="100" height="75" /> <span class="style1">&nbsp;<span class="style2">                   Dont Stop Believin: <span class="style4">&euro;10</span></span></span><br />
  85.       <br /> 
  86.       <span class="style1">Quantity</span>
  87.        <input type="text" id="dbox" onChange="addnumbers()" value="0" size="3" maxlength="3">
  88. &nbsp;<span class="style1">Gift Wrap? </span>
  89. <label>
  90. <input name="gift222" type="checkbox" id="gift222" value="checkbox" />
  91. </label></td>
  92.   </tr>
  93.   <tr>
  94.     <td bgcolor="#FFFFFF"><img src="images/t5.jpg" alt="never forget" width="100" height="75" /> <span class="style1">&nbsp;<span class="style2"> Greece: <span class="style4">&euro;10</span></span></span><span class="style1"><br />
  95.       <br />
  96.       Quantity</span>
  97.       <input type="text" id="ebox" onChange="addnumbers()" value="0" size="3" maxlength="3">
  98. &nbsp;<span class="style1">Gift Wrap? </span>
  99. <label>
  100. <input name="gift2222" type="checkbox" id="gift2222" value="checkbox" />
  101. </label></td>
  102.   </tr>
  103.   <tr>
  104.     <td bgcolor="#FFFFFF"><img src="images/t6.jpg" alt="never forget" width="100" height="75" /> <span class="style1">&nbsp;<span class="style2"> South Korea's Got Seoul: <span class="style4">&euro;10</span></span></span><span class="style1"><br />
  105.       <br />
  106.       Quantity</span>
  107.        <input type="text" id="fbox" onChange="addnumbers()" value="0" size="3" maxlength="3">
  108. &nbsp;<span class="style1">Gift Wrap? </span>
  109. <label>
  110. <input name="gift22222" type="checkbox" id="gift22222" value="checkbox" />
  111. </label></td>
  112.   </tr>
  113.   <tr>
  114.     <td bgcolor="#FFFFFF"><img src="images/t7.jpg" alt="never forget" width="100" height="75" /> <span class="style1">&nbsp;<span class="style2"> Camping Is Intents: <span class="style4">&euro;10</span></span></span><span class="style1"><br />
  115.       <br />
  116.       Quantity</span>
  117.       <input type="text" id="gbox" onChange="addnumbers()" value="0" size="3" maxlength="3">
  118. &nbsp;<span class="style1">Gift Wrap? </span>
  119. <label>
  120. <input name="gift222222" type="checkbox" id="gift222222" value="checkbox" />
  121. </label></td>
  122.   </tr>
  123.   <tr>
  124.     <td bgcolor="#FFFFFF"><img src="images/t8.jpg" alt="never forget" width="100" height="75" /> <span class="style1">&nbsp;<span class="style2"> You Complete Me: <span class="style4">&euro;10</span></span></span><span class="style1"><br />
  125.       <br />
  126.       Quantity</span>
  127.        <input type="text" id="hbox" onChange="addnumbers()" value="0" size="3" maxlength="3">
  128. &nbsp;<span class="style1">Gift Wrap? </span>
  129. <label>
  130. <input name="gift2222222" type="checkbox" id="gift2222222" value="checkbox" />
  131. </label></td>
  132.   </tr>
  133.   <tr>
  134.     <td bgcolor="#FFFFFF"><img src="images/t9.jpg" alt="never forget" width="100" height="75" /> <span class="style1">&nbsp;<span class="style2"> Leave Lindsay A-Lohan: <span class="style4">&euro;10</span></span></span><br />
  135.         <br />
  136.       <span class="style1">Quantity</span>
  137.        <input type="text" id="ibox" onChange="addnumbers()" value="0" size="3" maxlength="3">
  138. &nbsp;<span class="style1">Gift Wrap? </span>
  139. <label>
  140. <input name="gift22222222" type="checkbox" id="gift22222222" value="checkbox" />
  141. </label></td>
  142.   </tr>
  143.   <tr>
  144.     <td bgcolor="#FFFFFF"><img src="images/t10.jpg" alt="never forget" width="100" height="75" /> <span class="style1">&nbsp;<span class="style2"> Blimpin' Ain't Easy: <span class="style4">&euro;10</span></span></span><br />
  145.         <br />
  146.       <span class="style1">Quantity</span>
  147.        <input type="text" id="jbox" onChange="addnumbers()" value="0" size="3" maxlength="3">
  148. &nbsp;<span class="style1">Gift Wrap? </span>
  149. <label>
  150. <input name="gift222222222" type="checkbox" id="gift222222222" value="checkbox" />
  151. </label></td>
  152.   </tr>
  153. </table>
  154. </form>
  155.  
Oct 31 '07 #1
Share this Question
Share on Google+
4 Replies


iam_clint
Expert 100+
P: 1,208
you need some default values for those
NaN = "Not a Number"!
Oct 31 '07 #2

P: 47
you need some default values for those
NaN = "Not a Number"!
can you explain what you mean?

i had values set on the variables, and it wasn't taking the vaule i set, if i set the value to 1, it was giving it a value of 10.

the values are set on the radio buttons themselves.
Nov 1 '07 #3

P: 47
ok, i've figured it out, thanks.
Nov 1 '07 #4

acoder
Expert Mod 15k+
P: 16,027
ok, i've figured it out, thanks.
Can you post how you solved it?
Nov 1 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.