Click to See Complete Forum and Search --> : Sums


bloke
08-19-2003, 06:20 AM
Folks

Has anyone got a quick function to add the values from text fields?

I want to do it as I go so intend to use onblur to activate the function and post the running total back to total field.

Any clues?


Cheers

Charles
08-19-2003, 06:52 AM
Don't actually release this on the internet - JavaScript is way too iffy - but for demonstration purposes:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>
<style type="text/css">
<!--
input {display:block}
-->
</style>
<form action="">
<div>
<input type="text" name="one" onchange="this.form.total.value = Number(this.value) + Number(this.form.two.value)">
<input type="text" name="two" onchange="this.form.total.value = Number(this.value) + Number(this.form.one.value)">
<hr>
<input type="text" name="total">
</div>
</form>

bloke
08-19-2003, 06:56 AM
Originally posted by Charles
[B]Don't actually release this on the internet - JavaScript is way too iffy - but for demonstration purposes:

Do you have an alternative to javascript then?

Charles
08-19-2003, 06:57 AM
Submit the form to a server side script which returns the form but updated.

bloke
08-19-2003, 07:00 AM
Yeah, thought as much. Wanted to avoid that hence the javascript.

It's only for use on an intranet anyway so I know javascript will be supported for all of the users.

Cheers for your help.

Khalid Ali
08-19-2003, 10:35 AM
Originally posted by Charles
Submit the form to a server side script which returns the form but updated.

Cmon charles..that will be killing the bandwidth..if some one has validations as in this case where you have to validate form fields on every onclick or onblur or on change..god just imagine the activity that will generate towards and from the server..

But hey I am a JavaScript lover so what can I say..:-):D :D :D

bloke
08-19-2003, 10:41 AM
Managed it with this:

<SCRIPT LANGUAGE=javascript>
var total = 0;

function addVal(obj)
{
//parseInt FUNCTION CONVERTS DATA INTO INTEGERS
total = parseInt(total) + parseInt(obj.value);
document.purchase.totalvalue.value = total;
}

</script>

Then:

<td>&pound;&nbsp;<input type="text" name="total" size="10" maxlength="20" class="contentdetailnew" onBlur="addVal(this);"></td>

And:

<td colspan=2>&pound;&nbsp;<input type="text" name="totalvalue" size="30" maxlength="30" class="contentdetailnew" readonly></td>


Hunky Dory!