473,383 Members | 1,876 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,383 software developers and data experts.

writing a variable to a page

347 100+
Hi

I have the following code and it works fine if i want the total to be in a form field but i want to write the amount to the webpage using document.write, have tried all sorts but cant get it to work

Expand|Select|Wrap|Line Numbers
  1.          <script type="text/javascript">
  2. function calculate(f)
  3. {
  4. var nums = f.num;
  5. var ntext = f.num;
  6. var result = 0;
  7. for(var i=0;i<nums.length;i++)
  8. {
  9. if(nums[i].checked)
  10. {
  11. result+=parseFloat(ntext[i].value);
  12. }
  13. }
  14. f.answer.value=Number(result).toFixed(2);
  15. }
  16. </script>
can anyone help???
Nov 17 '09 #1
15 2577
Dormilich
8,658 Expert Mod 8TB
don’t use document.write(), use .appendChild() or .innerHTML (…).
Nov 17 '09 #2
colinod
347 100+
Hi

Thanks for the advice, i am not really that good with javascript, better with asp but that wont help, can anyone either help with some code or point me to somewhere to read about it, thanks
Nov 18 '09 #3
Dormilich
8,658 Expert Mod 8TB
if anyone would know, what you’re up to, there sure is some help possible. (the function does not explain much)
Nov 18 '09 #4
colinod
347 100+
Sorry about that....

I am making a page that has a form with multiple checkboxes on it and when you check the box it adds the amount set to that box to a total, thats what this code does, what i want to do is be able to send that total to another page so that i can seperate the list of items in the form out into seperate types
Nov 18 '09 #5
Dormilich
8,658 Expert Mod 8TB
update the total
Expand|Select|Wrap|Line Numbers
  1. // box refers to a representive checkbox
  2.  
  3. var total = 0;
  4.  
  5. function addAmount()
  6. {
  7.   if (this.checked)
  8.   { // if the box is checked, add its value
  9.     total += parseFloat(this.value);
  10.   }
  11.   else
  12.   { // else subtract it
  13.     total -= parseFloat(this.value);
  14.   }
  15. }
  16.  
  17. // for the sake of simplicity I use 1 checkbox and the standard functions
  18. // repeat with every checkbox
  19. box.addEventListener("click", addAmount, false);
Nov 18 '09 #6
colinod
347 100+
is this not the same as i first put on this thread just slightly different, surely my original post holds the amount i want in the variable result, i just cant get it to write to a page, i had it working but it refreshed the page and just gave me the total, i want to change some text on the page so that it changes as you click on the checkboxes

using this code

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function calculate(f)
  3. {
  4. {
  5. var nums = f.num;
  6. var ntext = f.num;
  7. var result = 0;
  8. for(var i=0;i<nums.length;i++)
  9. {
  10. if(nums[i].checked)
  11. {
  12. result+=parseFloat(ntext[i].value);
  13. }
  14. }
  15. f.answer.value=Number(result).toFixed(2);
  16. }
  17. document.write(result);
  18. }
  19. </script>
Nov 19 '09 #7
Dormilich
8,658 Expert Mod 8TB
@colinod
slight, but crucial differences

@colinod
If I knew, where (in terms of HTML) you want to output it, I could have thought of that earlier.

for now lets assume the simple case it were a text field.
Expand|Select|Wrap|Line Numbers
  1. var total = 0;
  2. var out = document.getElementById("total");
  3.  
  4. function addAmount()
  5. {
  6.   if (this.checked)
  7.   { // if the box is checked, add its value
  8.     total += parseFloat(this.value);
  9.   }
  10.   else
  11.   { // else subtract it
  12.     total -= parseFloat(this.value);
  13.   }
  14.   out.value = total;
  15. }
  16. // event handling as above
edit: change the event type to "change"
Nov 19 '09 #8
colinod
347 100+
Hi

Thanks but sorry to be a pain, i just cant see how this is different for getting the total in a variable, the page for all this is at

http://www.yaketyyakallmouth.com/radiousefee/index.asp

i populate all the checkboxes from a database in asp and just want to send the total from one page to another

as i said i am useless at javascript!!!!!
Nov 19 '09 #9
Dormilich
8,658 Expert Mod 8TB
@colinod
this will update the totals field when one of the checkboxes is clicked/changed.
in the end you will always have a form field with the correct total.

and the total is also saved in a global variable as well (accessible from everywhere)

@colinod
submit the form?

@colinod
no need to worry, I started there as well.
Nov 19 '09 #10
colinod
347 100+
yes i thought of submitting the form but if you look at the page we want to seperate all the sections in the right column into their own pages and have tabs for them at the top of the page and i did not think you could submit the form to various pages
Nov 19 '09 #11
Dormilich
8,658 Expert Mod 8TB
you can have tabs AND all the checkboxes in the same page. if you’re only submitting the total, you can use a separate form element just for the submit button.

EDIT: I can’t think of a way without using Javascript…
Nov 19 '09 #12
colinod
347 100+
so i could give the page for the action on eack button or tab using javascript and abviously retrieve the total from the textbox in the form on the next page?
Nov 19 '09 #13
Dormilich
8,658 Expert Mod 8TB
you’ve lost me there
Nov 19 '09 #14
colinod
347 100+
bad typing, i just meant i could set the page for the form action using javascript and get the totals from the form in the next page

just need to figure out how to set the action page for the form now
Nov 19 '09 #15
Dormilich
8,658 Expert Mod 8TB
out of interest I did a benchmark on the two approaches.

result (tried with 1,000 and 10,000 elements):
adding up all values took between 100 and 111 milliseconds
adding a single value took between 0 and 10 milliseconds

the code I used
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. // two div elements
  3. var foo = document.getElementById("foo");
  4. var bar = document.getElementById("bar");
  5.  
  6. // create 10,000 elements to query later
  7. var p = document.createElement("span");
  8. p.innerHTML = "1";
  9. for (var i=0; i<10000; i++) {
  10.     foo.appendChild(p.cloneNode(true));
  11. }
  12. p_list = foo.getElementsByTagName("span");
  13.  
  14. // test 1: add a single value
  15. function test1()
  16. {
  17.     var anf = (new Date).getMilliseconds();
  18.     run : {
  19.         total = parseInt(document.getElementById("ich").value);
  20.         total += parseInt(this.firstChild.data);
  21.     }
  22.     var end = (new Date).getMilliseconds();
  23.     alert("Sum: " + total + " in " + (end-anf) + " ms");
  24. }
  25. // test 2: sum up over all elements
  26. function test2()
  27. {
  28.     var total = 0;
  29.     var anf = (new Date).getMilliseconds();
  30.     for (var i=0; i<10000; i++) {
  31.         total += parseInt(p_list[i].firstChild.data);
  32.     }
  33.     var end = (new Date).getMilliseconds();
  34.     alert("Sum: " + total + " in " + (end-anf) + " ms");
  35. }
  36. // start tests on click
  37. p_list[1].addEventListener("click", test1, false);
  38. bar.addEventListener("click", test2, false);
  39. </script>
Dec 8 '09 #16

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

Similar topics

4
by: George Stout | last post by:
First off I do not know alot about writing queries to an Access Database from an ASP page. This is why I need help. I have an Events database for 6 colleges in our metro area. On the homepage I...
9
by: Kelly Vernon | last post by:
I have a standard ASP page that appends to an xml page. Currently if there is more than one person attempting to append to the same XML file at a time. One user will have the ability to append,...
9
by: curious_one | last post by:
All, I have a struct struct { char a; char b; }some_struct; I have a shared memory that can contain 16bit wide data, I find that when writing an 8bit value in to char "a" the same value is...
2
by: estafford | last post by:
I am having trouble writing a conditional block using ASP.NET and C#. I am trying to do something like this: 1. if page is PostBack - transfer to another page 2. if not postback - connect...
5
by: Michelle A. | last post by:
I have a four page form. Pages 1-3 stores there information in a session variables. Page four is reached (a final review page) and then the information will be written to the SQL server. When...
3
by: Csaba Gabor | last post by:
Firefox's configuration settings (Prefs.js) can be accomplished via the interface at about:config. Q1. Is there any such setting that can be repeatedly altered via javascript (in a vanilla...
3
by: jonnyblazed | last post by:
Okay..... I'm gonna sum up what I want to do cause it will make it pretty simple for you guys to understand. Here we go: I have a form that posts data to a PHP mailer page I have created. When...
7
by: miladhatam | last post by:
hi i am milad and i have started learning asp.net c# for 2 months how can i send parameter to a page with code i did this work with html by writing the destination address in action thanks
1
by: Julian32 | last post by:
Hello, I'm trying to keep track of purchases made from my website by having Paypal redirect users to a page on my site after purchase, and writing the information received to an Access Database....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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:
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.