473,664 Members | 2,797 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

adding a value

47 New Member
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 1697
iam_clint
1,208 Recognized Expert Top Contributor
you need some default values for those
NaN = "Not a Number"!
Oct 31 '07 #2
ballygowanboy
47 New Member
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
ballygowanboy
47 New Member
ok, i've figured it out, thanks.
Nov 1 '07 #4
acoder
16,027 Recognized Expert Moderator MVP
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
2404
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 abc 2 abc 3
10
4003
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 I add the second row, instant. How can I eliminate this delay?
2
369
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 required values as needed and setting them to the relevant property within my object. The reading of the xml file works as it should. I get a problem when I try to add the object that I created to the arraylist. It seems to be adding each one,...
9
10888
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 the wheel, right? Everything so far is working well with the Active Directory. The problem I am having is with adding File Permissions to a directory. I am currently using some code courtesy of "Willy Denoyette "
3
4872
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 the best method? Do you have a sample of how to do this?
3
1944
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 example would be: Let's say Column1=StartDate and Column2=EndDate. In addition to displaying Column1 and Column2, I need to do some calculations and display in as Column3. The calculations are easy and can be done in the code-behind. How to display...
0
1871
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 collect the data in the Textboxes which i added dynamically from server side, i am not able to do . Alos i tried to bedug, the total no of rows in Html table does not reflect the dynamically added rows.
6
4408
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 row doesn't get filled. I have a connection and all that stuff. Private Sub btnPlaceBet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlaceBet.Click ' Dim Myds As Footbet.DStable '...
47
3347
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 are objects whose representation within the code is more important than their actual value. Two symbols needs only to be
2
2100
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 but for other browser like-firefox,opera its not working......... i m giving u my complete xml,xsl and transformation code....u only paste and check why it is not working for other browser.....and correct it and let me know......this is really a big...
0
8437
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8861
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8778
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8549
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8636
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7375
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4185
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4351
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2003
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.