View Full Version : Form Variables?

Mar 25th, 2003, 05:27 PM
I have 4 text input fields in which the user types in 4 amounts. I also have a variable called "totalsum" which will give me the total sum of the inputed figures. While this works ok, i would like the total to be sent via a hidden form field in a form, but im not sure what to put in the value field.


Thanks in advance...

Mar 25th, 2003, 08:25 PM
Something like this?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<meta name="Generator" content="TextPad 4.4" />
<meta name="Author" content="Peter Bailey" />

<script type="text/javascript">

function showTotal( f )
var fields = ['value1','value2','value3','value4'];
var total = 0;

for ( var elem, i = 0; ( elem = f.elements[fields[&#105;]] ); i++ )
if ( isNaN( elem.value ) )
elem.value = 0;
total += parseFloat( elem.value );

f.elements['total'].value = total;




Value 1 <input type="text" name="value1" value="0" onblur="showTotal( this.form )" /><br />
Value 2 <input type="text" name="value2" value="0" onblur="showTotal( this.form )" /><br />
Value 3 <input type="text" name="value3" value="0" onblur="showTotal( this.form )" /><br />
Value 4 <input type="text" name="value4" value="0" onblur="showTotal( this.form )" /><br />
<hr />
Total <input type="text" name="total" value="0" readonly="true" />

</html>Note: I made the total as text not hidden so you can see what's going on. Just change it to hidden for application

Also, do you want to prevent the other 4 values from submitting as well?

Mar 25th, 2003, 08:28 PM
<script language="javascript">
function sum(myform){
myform.total.value=parseInt(myform.box1.value)+parseInt(myform.box2.value)+parseInt(myform.box3.valu e)+parseInt(myform.box4.value)
<form onsubmit="sum(this);">
<input type="text" name="box1"><br>
<input type="text" name="box2"><br>
<input type="text" name="box3"><br>
<input type="text" name="box4"><br>
<input type="hidden" name="total"><br>
<input type="submit">