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

Dynamically adding numbers from a form.

Hello....disclaimer...I am a newbie :)

I am attempting to add 2 fields and obtain a total sum

Field 1 would be a list field. Showing 4 choices
Each choice has a number association (ie choice 1 =40, choice 2 = 80, etc)

Field 2 would be another list field...showing 4 choices
If choice 1 = 40, if choice 2 = 80 ,etc)

Summary Calc would add Field 1 and Field 2 together. I would prefer to do this without changing pages so I am guessing javascript would be the best choice....

Any help would be appreciated...thanks!

Greg
Aug 31 '07 #1
17 2473
acoder
16,027 Expert Mod 8TB
Welcome to TSDN!

Give the select options values (just in case you haven't), e.g.
Expand|Select|Wrap|Line Numbers
  1. <option value="40">Choice 1
Also give all three elements ids, e.g.
Expand|Select|Wrap|Line Numbers
  1. <select id="field1">
To add, try:
Expand|Select|Wrap|Line Numbers
  1. var field1 = document.getElementById("field1").value;
  2. var field2 = document.getElementById("field2").value;
  3. document.getElementById("sumCalc").value=field1+field2;
How do you wish to trigger this? Whenever an option is changed or on clicking a button?
Sep 1 '07 #2
Hello and THANKS for helping!

My idea was that the form total would dynamically update anytime either list was altered. The goal was to general the totalfee amount so the person would know their fees and the amount could be sent to paypal payflow link for credit card payment.

Here is the general idea:

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  5. <title>Untitled Document</title>
  6. </head>
  7.  
  8. <body>
  9. <form id="form1" name="form1" method="post" action="">
  10.   <table width="700" border="1">
  11.     <tr>
  12.       <td><div align="right">Category</div></td>
  13.       <td><select name="select3" id="select3">
  14.         <option>Select a Category</option>
  15.         <option value="85">OMEA Member</option>
  16.         <option value="25">OCMEA Member</option>
  17.         <option value="45">Saturday Only</option>
  18.       </select></td>
  19.       <td>&nbsp;</td>
  20.     </tr>
  21.     <tr>
  22.       <td><div align="right">Family Attending</div></td>
  23.       <td><select name="select4" id="select4">
  24.         <option value="0">0</option>
  25.         <option value="40">1</option>
  26.         <option value="80">2</option>
  27.         <option value="120">3</option>
  28.       </select></td>
  29.       <td>&nbsp;</td>
  30.     </tr>
  31.     <tr>
  32.       <td><div align="right">Total Fees</div></td>
  33.       <td><input type="text" name="textfield" id="textfield" /></td>
  34.       <td>&nbsp;</td>
  35.     </tr>
  36.   </table>
  37.   <p>&nbsp;</p>
  38. </form>
  39. </body>
  40. </html>
Sep 1 '07 #3
acoder
16,027 Expert Mod 8TB
Define a function, e.g.
Expand|Select|Wrap|Line Numbers
  1. function addfields() {
  2.  var select3 = document.getElementById("select3").value;
  3.  var select4 = document.getElementById("select4").value;
  4.  document.getElementById("textfield").value=select3+select4;
  5. }
Use onchange on both select elements to call this:
Expand|Select|Wrap|Line Numbers
  1. <select ... onchange="addfields()">
Sep 1 '07 #4
Thank you again for your help!

Unfortunately, I guess I wasn't clear in what I meant by adding the numbers...sorry

I need the numbers to be added mathmatically. Your solution works perfectly for concatenating the two fields....unfortunately I am looking for the sum of the two numbers.

Thanks again for any assistance.

Greg
Sep 2 '07 #5
markrawlingson
346 Expert 100+
If it's concatening the two numbers javascript is treating the numbers as strings instead of integers. You'll need to tell javascript that they are numbers, then it will add them properly.

Expand|Select|Wrap|Line Numbers
  1. function addfields() {
  2.  var select3 = parseInt(document.getElementById("select3").value);
  3.  var select4 = parseInt(document.getElementById("select4").value);
  4.  document.getElementById("textfield").value=select3+select4;
  5. }
  6.  
Note the existence of parseInt() wrapped around the values coming from the textboxes. If this doesn't work then try also wrapping is around the variables while you're performing the mathematical statement : parseInt(select3) + parseInt(select4)
Sep 2 '07 #6
Mark

That did the trick...THANK YOU BOTH for helping. I really appreciate it!!!
Final 2 questions.....

1. Is there a way to have the sumCalc field be none manually editable to the user (sort of like an echo in php)?

2. Is there a way to do an "if" statement to prevent any value from showing up if the user selects the inital select default (ie. "Please Select A Category")


Thanks for helping a newbie...I appreciate it. I am progressing nicely on php but I am still new to javascript.

Greg
Sep 2 '07 #7
markrawlingson
346 Expert 100+
Is there a way to have the sumCalc field be none manually editable to the user
You mean non-manually editable? So they can't enter anything into the field... ?

If that's what you mean then.. Sure there is. But that's just simple HTML.

<input type="text" DISABLED>
Sep 2 '07 #8
gits
5,390 Expert Mod 4TB
hi ...

yep ... disabled would do the trick and another option would be to set the readonly-attribute:

Expand|Select|Wrap|Line Numbers
  1. <!-- makes it really disabled - you cannot even copy the value -->
  2. <input type="text" id="whatever_id" disabled="disabled"/>
  3.  
  4. <!-- makes it readonly - you may focus/select/copy the value -->
  5. <input type="text" id="whatever_id" readonly="readonly"/>
  6.  
that is the html way - and in case you want to set it through javascript you have to set/remove the attribute:

Expand|Select|Wrap|Line Numbers
  1. // to make it readonly
  2. node_ref.setAttribute('readonly', 'readonly');
  3.  
  4. // to make it editable
  5. node_ref.removeAttribute('readonly');
but you may also set the property of the obj:

Expand|Select|Wrap|Line Numbers
  1. // to make it readonly
  2. node_ref.readonly = true;
  3.  
  4. // to make it editable
  5. node_ref.readonly = false;
and it would be good practice not to mix the ways of setting the states ... that means use the attribute-way OR the properties-way ... believe me ... as your project and codebase grows you would be glad when having only one way ... because setting the property wouldn't change the attribute or its value ...

kind regards
Sep 2 '07 #9
Once again, thank you both! It works like a charm!!

....the last issue would be if a user selects on option in the first select menu, then reverts back to the default value "Please select a category", the sumCalc field shows "NaN" as the result....is there a way to wipe out the sumCalc result if the first select menu is the default?

I really appreciate you taking time to help a newbie. I have learned quite a bit just from you...thanks again!

Greg
Sep 2 '07 #10
gits
5,390 Expert Mod 4TB
hi ...

try something like the following:

Expand|Select|Wrap|Line Numbers
  1. function addfields() {
  2.     var select3 = parseInt(document.getElementById('select3').value);
  3.     var select4 = parseInt(document.getElementById('select4').value);
  4.     var sum_field = document.getElementById('textfield');
  5.  
  6.     var value = select3 + select4;
  7.  
  8.     sum_field.value = isNaN(value) ? '' : value;
  9. }
kind regards
Sep 2 '07 #11
A perfect solution..THANK YOU!


Do you have any suggestions for javascript books? I would really like to become more knowledgeable on the topic.

Thanks and best wishes

Greg
Sep 2 '07 #12
gits
5,390 Expert Mod 4TB
hi ...

JavaScript. The Definitive Guide by David Flanagan (5th Edition) ... is always a good recommendation ;) ... be sure about the edition ... the 5th is the latest one.

kind regards
Sep 2 '07 #13
Thanks! Hope you enjoy the rest of the holiday weekend!

Greg
Sep 2 '07 #14
gits
5,390 Expert Mod 4TB
no problem :) ... post back to the forum anytime you have more questions

kind regards
Sep 2 '07 #15
acoder
16,027 Expert Mod 8TB
Unfortunately, I guess I wasn't clear in what I meant by adding the numbers...sorry

I need the numbers to be added mathmatically. Your solution works perfectly for concatenating the two fields....unfortunately I am looking for the sum of the two numbers.
Drats, I forgot about the parseInt. Never mind, others pointed it out.
Sep 2 '07 #16
acoder
16,027 Expert Mod 8TB
....the last issue would be if a user selects on option in the first select menu, then reverts back to the default value "Please select a category", the sumCalc field shows "NaN" as the result....is there a way to wipe out the sumCalc result if the first select menu is the default?
If you just want a zero instead, you could set the value of the first option to 0 for an easier solution.
Sep 2 '07 #17
acoder
16,027 Expert Mod 8TB
Do you have any suggestions for javascript books? I would really like to become more knowledgeable on the topic.
Don't forget to also check out the tutorial links in the Offsite Links thread.
Sep 2 '07 #18

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

Similar topics

10
by: sp0 | last post by:
Is there a reason why to make mix numbers improper when adding? It seems when subtracting and adding, adding a subtracting the whole numbers and fraction parts should be sufficient? what'ch think
5
by: surrealtrauma | last post by:
the requirement is : Create a class called Rational (rational.h) for performing arithmetic with fractions. Write a program to test your class. Use Integer variables to represent the private data...
6
by: rcopeman | last post by:
I need to add two number together and check that the value does not exceed a third value as a form is being filled in. Can I do this with onfocusout? If so how?! Thanks for any help.
8
by: Donald Xie | last post by:
Hi, I noticed an interesting effect when working with controls that are dynamically loaded. For instance, on a web form with a PlaceHolder control named ImageHolder, I dynamically add an image...
19
by: matt | last post by:
I've seen several posts that begin to address this problem, but have not found a simple, elegant solution that will accomplish this goal. The important part of this solution is that it must be...
7
by: Steve_Black | last post by:
Hello, I'm toying with the idea of loading a MenuStrip (VB.Net 2005) dynamically based on who is logged into my system. Every user has different security settings and I want to customize the...
3
by: Mark Denardo | last post by:
I'm trying to dynamically create and add controls to a web page: Label obj1 = new Label(); DropDownList obj2 = new DropDownList(); Controls.Add(obj1); Controls.Add(obj2); But I get the...
12
by: vbnewbie | last post by:
I am having problems accessing properties of dynamically generated objects in VB2005. Can someone please help? In a nutshell: My app creates an equal number of checkboxes and labels that share the...
1
by: Paddy | last post by:
The problem I am facing is as follows: I am populating an HTML table on my webpage with rows of data from a database. The rows may be sometimes 10 and sometimes say,3. I have two buttons on that...
1
by: semomaniz | last post by:
I have a form where i have created the form dynamically. First i manually added a panel control to the web page. Then i added another panel dynamically and inside this panel i created tables. I have...
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
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: 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
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
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.