CodeCharge Studio
search Register Login  

Visual Web Reporting

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> General/Other

 How do I dynamically update calculated control values(Client Event)

Print topic Send  topic

Author Message
Livinus

Posts: 48
Posted: 05/01/2009, 2:52 PM

Hello all

I am new to javascript as I use server events mostly for my apps. Currently i have a need to change the value of some controls and display the result on the affected controls before submiting the form. I have a form with the following controls: total,rate,other_charges,discount , amount_paid & balance all related as follwows:

1. total=rate+other_charges+(discount/100*rate)
2. balance=total-amount_paid

I want to attach a script to the onchange event of each control to display the calculated values values on the form dynamically each time values change for the other controls before submitting the values for storage in the database

I would appreciate any help.
View profile  Send private message
melvyn


Posts: 333
Posted: 05/01/2009, 8:48 PM

let's say your form is named Form1.

<form name="Form1" .... >

The onchange event on each control will look like this:
onchange="calculateTotal();"

Then let's do the function to calculate the total:
  
function calculateTotal(){  
  rate = document.forms['Form1'].rate.value;  
  other = document.forms['Form1'].other.value;  
  discount = document.forms['Form1'].discount.value;  
   
  total = rate + other + (discount/100*rate);  
  document.forms['Form1'].total.value = total;  
  
  amount_paid = document.forms['Form1'].amount_paid.value;  
  balance = total - amount_paid;  
  
  document.forms['Form1'] .balance.value = balance;  
}  

That's all. The code is self-explaining. It's as clear as plain language :-)
_________________
Melvyn Perez
Puro Codigo
http://purocodigo.com
View profile  Send private message
Livinus

Posts: 48
Posted: 05/04/2009, 4:34 PM

Hi melvyn,

You've made my day. I am a newbie in javascript but your codes are just too clear for me to fail to grasp what it is doing. I however want to ask where to attach the function. I mean which event?. Is it the form's onload event or where exactly? Thanks for your assistance.
View profile  Send private message
damian

Posts: 838
Posted: 05/04/2009, 8:24 PM

usually you will add the function in the page onload and then call it as required.
_________________
if you found this post useful take the time to help someone else.... :)
View profile  Send private message
melvyn


Posts: 333
Posted: 05/05/2009, 12:43 PM

Quote Livinus:
I however want to ask where to attach the function. I mean which event?. Is it the form's onload event or where exactly? Thanks for your assistance.

Well, you want the function fired when a field change.

  
<input type="text" size="14" name="amount_paid" onchange="calculateTotal();" />  

This is clear: the function will fire when the field amount_paid change its content.

If you reffer to a client side event from CCS don't care. Forget it and insert this in the field.
_________________
Melvyn Perez
Puro Codigo
http://purocodigo.com
View profile  Send private message
Livinus

Posts: 48
Posted: 05/07/2009, 11:13 PM

Thank you Melvyn & Damian I appreciate your posts. I am sure the codes will be useful to many others in future just as they have been to me.
View profile  Send private message

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

Internet Database

Visually create Web enabled database applications in minutes.
CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.