sverne
asked on
Get form field value using an offset in a Function
I have a Form created using Perl that has a table with a row for each invoice in a large table. I display all the rows in a table and allow the user to edit some info for each record:
for ($item_cntr = 0; $cntr < $record_cnt; ++$item_cntr)
{ print qq(<tr>
<td><input type="text" name="c_freight_$item_cntr " value="$i_freight[$item_cn tr]" onBlur="linetotal($item_cn tr)" ></td>
<td><input type="text" name="c_miscamt_$item_cntr " value="$i_miscamt[$item_cn tr]" ></td>
<td><input type="text" name="c_price_$item_cntr" value="$i_price[$item_cntr ]" ></td>
</tr>);
}
I want to create a Function that is called when they change the Freight value and that causes the rows total Price (freight + miscamt = price) to be calculated again (without hitting a Submit button on the Form).
I created a Function:
function linetotal(whatline) {
var fixed_freight = "document.editform.c_freig ht_" + whatline;
var fixed_misc = "document.editform.c_misca mt_" + whatline;
var linetotal = parseFloat(fixed_freight.v alue) + parseFloat(fixed_misc.valu e);
document.editform.c_price_ whatline.v alue = linetotal; } }
So, if the first row in the table is 0, then the Function is passed "0" and should get the value of the form fields:
'c_freight_0' and 'c_miscamt_0' and set 'c_price_0' to a new value in the form.
The fixed_freight is showing that the string it has is correct, but it does not get the value from the form. Any ideas?
for ($item_cntr = 0; $cntr < $record_cnt; ++$item_cntr)
{ print qq(<tr>
<td><input type="text" name="c_freight_$item_cntr
<td><input type="text" name="c_miscamt_$item_cntr
<td><input type="text" name="c_price_$item_cntr" value="$i_price[$item_cntr
</tr>);
}
I want to create a Function that is called when they change the Freight value and that causes the rows total Price (freight + miscamt = price) to be calculated again (without hitting a Submit button on the Form).
I created a Function:
function linetotal(whatline) {
var fixed_freight = "document.editform.c_freig
var fixed_misc = "document.editform.c_misca
var linetotal = parseFloat(fixed_freight.v
document.editform.c_price_
So, if the first row in the table is 0, then the Function is passed "0" and should get the value of the form fields:
'c_freight_0' and 'c_miscamt_0' and set 'c_price_0' to a new value in the form.
The fixed_freight is showing that the string it has is correct, but it does not get the value from the form. Any ideas?
ASKER
Error on the fixed_misc line
<html><head>
<script type="text/javascript" language="JavaScript">
function linetotal(theField) {
var whatline = theField.name.replace(/\D/g,"");
var theForm = theField.form;
var fixed_freight = theField.value;
var fixed_misc = theForm["c_miscamt_" + whatline].value;
var total = parseFloat(fixed_freight) + parseFloat(fixed_misc);
theForm["c_price_"+whatline].value = total.toFixed(2);
}
</script></head><body>
<table>
<tr>
<td><input type="text" name="c_invoice_0" value='123'></td>
<td><input type="text" name="c_freight_0" value='1.00' onBlur="linetotal(this)" ></td>
<td><input type="text" name="c_miscamt_0" value='2.00'></td>
<td><input type="text" name="c_price_0" value='3.00'></td>
</tr>
<tr>
<td><input type="text" name="c_invoice_1" value='124'></td>
<td><input type="text" name="c_freight_1" value='17.00' onBlur="linetotal(this)" ></td>
<td><input type="text" name="c_miscamt_1" value='20.00'></td>
<td><input type="text" name="c_price_1" value='37.00'></td>
</tr>
<tr>
<td><input type="text" name="c_invoice_2" value='125'></td>
<td><input type="text" name="c_freight_2" value='5.00' onBlur="linetotal(this)" > </td>
<td><input type="text" name="c_miscamt_2" value='6.00'></td>
<td><input type="text" name="c_price_2" value='11.00'></td>
</tr>
</table></body></html>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Problem resolved and the code is attached below
<html><head>
<script type="text/javascript" language="JavaScript">
function linetotal(theField) {
var whatline = theField.name.replace(/\D/g,"");
var theForm = theField.form;
var fixed_freight = theField.value;
var fixed_misc = theForm["c_miscamt_" + whatline].value;
var total = parseFloat(fixed_freight) + parseFloat(fixed_misc);
theForm["c_price_"+whatline].value = total.toFixed(2);
}
</script></head><body>
<form method="POST" action="rb_return_create_test.pl" name="editform">
<table>
<tr>
<td><input type="text" name="c_invoice_0" value='123'></td>
<td><input type="text" name="c_freight_0" value='1.00' onBlur="linetotal(this)" ></td>
<td><input type="text" name="c_miscamt_0" value='2.00'></td>
<td><input type="text" name="c_price_0" value='3.00'></td>
</tr>
<tr>
<td><input type="text" name="c_invoice_1" value='124'></td>
<td><input type="text" name="c_freight_1" value='17.00' onBlur="linetotal(this)" ></td>
<td><input type="text" name="c_miscamt_1" value='20.00'></td>
<td><input type="text" name="c_price_1" value='37.00'></td>
</tr>
<tr>
<td><input type="text" name="c_invoice_2" value='125'></td>
<td><input type="text" name="c_freight_2" value='5.00' onBlur="linetotal(this)" > </td>
<td><input type="text" name="c_miscamt_2" value='6.00'></td>
<td><input type="text" name="c_price_2" value='11.00'></td>
</tr>
</table></form></body></html>
Change your onBlur event handler to this: onBlur="linetotal(this)"
Then your script goes like this:
function linetotal(theField) {
var whatline = theField.name.replace(/\D/
var theForm = theField.form;
var fixed_freight = theField.value;
var fixed_misc = theForm["c_miscamt_" + whatline].value;
var total = parseFloat(fixed_freight) + parseFloat(fixed_misc);
theForm["c_price_"+whatlin
}