Forum Moderators: open

Message Too Old, No Replies

Function to update one field in another form from totals

         

timchuma

4:10 am on Jul 8, 2004 (gmt 0)

10+ Year Member Top Contributors Of The Month



I saw a similar function to this used on another site, but the value it was updating was in the same form.

Due to the way the input form works on that page, I want the function to be able to update a value on a different form.

Eg.


x y Quantity
a b 1
c d 1
2 : Total Quantity


function CalculateTotal(frm) {
var Total_Quantity = 0
for (var i=0; i < frm.elements.length; ++i) {

form_field = frm.elements[i]

form_name = form_field.name

if (form_name.substring(0,8) == "Quantity") {

item_quantity = parseInt(form_field.value)

if (item_quantity >= 0) {
Total_Quantity += item_quantity
}
}
}
document.forms.totalform.TotalQuantity.value = Total_Quantity
}

For some reason it keeps saying "document.forms.totalform.TotalQuantity.value" is null or not an object even if I pass that form object to the function in the first place (the function is called when one of the values is updated on the first form.)

Thanks.

Bernard Marx

9:06 am on Jul 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think we may need to see the form too.

timchuma

10:08 pm on Jul 8, 2004 (gmt 0)

10+ Year Member Top Contributors Of The Month



The forms look like this (from an autogenerated example):


<form ACTION="moreinfo.asp" NAME="cart" METHOD="post">

<table width="97%" border="0" cellspacing="2" cellpadding="2" align="center">

<tr bgcolor="#990000" valign="middle">

<td align="center"><b><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">
PrintColour1 *
</font></b></td>

<td align="center"><b><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">
Colour1 *
</font></b></td>

<td align="center"><b><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">
Quantity *
</font></b></td>

</tr>
<tr><td><select name='PrintColour1_1'><OPTION VALUE="">
<OPTION VALUE="Black">Black
<OPTION VALUE="White">White
<OPTION VALUE="Grey">Grey
<OPTION VALUE="Red">Red
<OPTION VALUE="Pink">Pink
<OPTION VALUE="Mauve">Mauve
<OPTION VALUE="Purple">Purple
<OPTION VALUE="Blue">Blue
<OPTION VALUE="Light Blue">Light Blue
<OPTION VALUE="Green">Green
<OPTION VALUE="Light Green">Light Green
<OPTION VALUE="Yellow">Yellow
<OPTION VALUE="Burnt Yellow">Burnt Yellow
<OPTION VALUE="Orange">Orange
<OPTION VALUE="Blood Orange">Blood Orange
</select></td>
<td><select name='Colour1_1'><OPTION VALUE="">
<OPTION VALUE="Black">Black
<OPTION VALUE="White">White
<OPTION VALUE="Grey">Grey
<OPTION VALUE="Red">Red
<OPTION VALUE="Pink">Pink
<OPTION VALUE="Mauve">Mauve
<OPTION VALUE="Purple">Purple
<OPTION VALUE="Blue">Blue
<OPTION VALUE="Light Blue">Light Blue
<OPTION VALUE="Green">Green
<OPTION VALUE="Light Green">Light Green
<OPTION VALUE="Yellow">Yellow
<OPTION VALUE="Burnt Yellow">Burnt Yellow
<OPTION VALUE="Orange">Orange
<OPTION VALUE="Blood Orange">Blood Orange
</select></td>
<td><input type='text' name="Quantity_1" onChange="CalculateTotal(this.form)"></td>
<input type='hidden' name="ExtraInfoLine_1" value="1"></tr>
.
.
.
(etc)
<tr>
<td colspan="2">
</td><td>
<FORM ACTION="moreinfo.asp" NAME="totalform" METHOD="post">

<input type="text" name="TotalQuantity" value="0"> : <i>Total Quantity</i>
</form>
</td></tr>
<tr><td colspan="3"><input type="Submit" value="submit"></td></tr>
</form>
</table>

Bernard Marx

4:39 pm on Jul 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That had me confused for a little while.
The form, totalform, is currently nested inside the form, cart. Rearrange your HTML a little, and things will hopefully be sweeter!
[Feel free to give yourself a few slaps on the forehead in private :) ]