473,386 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

adding a value

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
4 1671
iam_clint
1,208 Expert 1GB
you need some default values for those
NaN = "Not a Number"!
Oct 31 '07 #2
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
ok, i've figured it out, thanks.
Nov 1 '07 #4
acoder
16,027 Expert Mod 8TB
ok, i've figured it out, thanks.
Can you post how you solved it?
Nov 1 '07 #5

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

Similar topics

6
by: Jamie Fryatt | last post by:
Hi everyone, here's what id like to do. I have a table with 2 fields, name and value I need to be able to add multiple records quickly, for example I need to add name value abc 1...
10
by: Eric Petruzzelli | last post by:
If I fill my dataset and there is no data. The dataset is still created with zero rows (all columns are there). When I add my first row using the script below, it takes over 2 seconds to add??? If...
2
by: Vishal Somaiya | last post by:
Hello I am trying read from a xml file, pull the values in a object and then add the object to an ArrayList. I am using a 'while' loop to move through each node in the xml file and pulling the...
9
by: Ben Dewey | last post by:
Project: ---------------------------- I am creating a HTTPS File Transfer App using ASP.NET and C#. I am utilizing ActiveDirectory and windows security to manage the permissions. Why reinvent...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
3
by: Robin Thomas | last post by:
I am fairly new to ASP.NET so I think I am missing something fundamental. Anyway, quite often I am pulling data from a database, but then I need to use that data to produce more data. A simple...
0
by: Sileesh | last post by:
Hi I have html table and a Button in an Aspx page. I am adding one row with some textboxes to Html table each time i click on the Button thru Javascript. Now problem is when when i try to...
6
by: Rudy | last post by:
Hi all, I know this is easy, just can't seem to get it. I have a windows form, and a text box, with a value already in it. I need to add that value to a table. It's just one value, so the entire...
47
by: Pierre Barbier de Reuille | last post by:
Please, note that I am entirely open for every points on this proposal (which I do not dare yet to call PEP). Abstract ======== This proposal suggests to add symbols into Python. Symbols...
2
by: abhimanyu singh | last post by:
i m facing a serious probs now a days............ its seems easy but for me i found it difficult......... i want to do transformation usig xsl and xml(adding parameter). for IE its working fine...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.