Adding the values of text boxes (when some text boxes are not called for by PHP)

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Dec 2007
Posts: 25
Reputation: mark2326l is an unknown quantity at this point 
Solved Threads: 0
mark2326l mark2326l is offline Offline
Light Poster

Adding the values of text boxes (when some text boxes are not called for by PHP)

 
-1
  #1
Oct 4th, 2009
This is my first project using JavaScript.
I have a PHP page that loads text fields "if" called for. I thought JavaScript would be a great way to get the total of all Text Fields without Submit.
The code below is an example of my problem.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <SCRIPT language = JavaScript>
  2.  
  3. function calculate() {
  4. A = document.frmOne.txtFirstNumber.value *1
  5. B = document.frmOne.txtSecondNumber.value *1
  6. C = document.frmOne.txtThirdNumber.value *1
  7. D = A + B + C
  8. document.frmOne.txtFourthNumber.value = D
  9. }
  10.  
  11. </SCRIPT>
  12.  
  13. <FORM NAME = frmOne>
  14.  
  15. Number One: <INPUT TYPE = Text NAME = txtFirstNumber SIZE = 5 value ="">
  16.  
  17. Number Two: <INPUT TYPE = Text NAME = txtSecondNumber SIZE = 5 value ="">
  18. <P>
  19. Total: <INPUT TYPE = Text NAME = txtFourthNumber SIZE = 5 value = "">
  20. <P>
  21. <Input Type = Button NAME = b1 VALUE = "Add Numbers" onClick = calculate()>
  22.  
  23. </FORM>
If I add
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <INPUT TYPE = Text NAME = txtThirdNumber SIZE = 5 value ="">
to the form, the code works fine.
But again my page dose not always call ALL the Fields in the JS code.

Can anyone help me with this?
Thanks
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,407
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 226
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is online now Online
Code Monkey

Re: Adding the values of text boxes (when some text boxes are not called for by PHP)

 
0
  #2
Oct 4th, 2009
You have C = document.frmOne.txtThirdNumber.value *1 in your function so if that field doesn't exist and you try to perform an action on it ( *1 in this case) the function will die.
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 25
Reputation: mark2326l is an unknown quantity at this point 
Solved Threads: 0
mark2326l mark2326l is offline Offline
Light Poster

Re: Adding the values of text boxes (when some text boxes are not called for by PHP)

 
0
  #3
Oct 4th, 2009
Thanks Shawn
But that didn't fix it.
The only way I can make the Form work the way it is written, is to delete C = document.frmOne.txtThirdNumber.value and +C from D = A + B + C in JS
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 25
Reputation: mark2326l is an unknown quantity at this point 
Solved Threads: 0
mark2326l mark2326l is offline Offline
Light Poster

Re: Adding the values of text boxes (when some text boxes are not called for by PHP)

 
0
  #4
Oct 4th, 2009
I think I got it
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var C ;
  2. if (window.document.frmOne.txtThirdNumber) {C=document.frmOne.txtThirdNumber.value * 1}
  3. else {C=0}
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC