Click to See Complete Forum and Search --> : form calculation
LuigiX
11-11-2003, 08:52 PM
Hi
I have a form with a 3 column table which holds items to be calculated as follows:
Col1 - a checkbox and item label
Col2 - a combobox allowing user to select no of items up to 5
Col3 - the per unit price of the item
Col4 - the price of the item (per item price X number from col2)
Last row (col4) will hold the total price of all items.
As a small complication the combobox in col2 will only be displayed if the item checkbox is selected in col1.
The form will send the name, number and price of each item, as well as the total price.
Thanks
Cheers
Luigi
skriptor
11-12-2003, 03:39 AM
Hi,
you made a great job, but what is your problem ?
skriptor
LuigiX
11-12-2003, 11:52 AM
Hi
I'm fairly new to javascript.
Do you have any code to do the calculations or can you point me in the right direction.
Cheers
Luigi
skriptor
11-14-2003, 02:59 AM
Hi,
try something like this:
<html><head>
<script language="javascript">
function show( what ) {
if ( document.getElementById( 'b' + what ).style.visibility == "hidden" ) {
document.getElementById( 'b' + what ).style.visibility = "";
} else {
document.getElementById( 'b' + what ).style.visibility = "hidden";
}
calc( what );
}
function calc( what ) {
var szValue;
var oSelect = document.getElementById( 'b' + what );
if ( oSelect.style.visibility == "hidden" ) {
szValue = "";
} else {
var fPrice = parseFloat( document.getElementById( 'c' + what ).childNodes[ 0 ].data );
var fMenge = oSelect.options[ oSelect.selectedIndex ].value;
szValue = (fPrice * fMenge).toFixed( 2 ) + " Euro";
}
var oTextNode = document.createTextNode( szValue );
var oParent = document.getElementById( 'd' + what );
oParent.replaceChild( oTextNode, oParent.childNodes[ 0 ] );
}
</script>
</head><body>
<form id="order" action="order.htm">
<table summary="orderform">
<tr><td>
article <input type="checkbox" id="a1" name="article" onclick="show('1');" />
</td><td>
<select id="b1" style="visibility:hidden" name="mode" onchange="calc( '1' );">
<option value="1"> 1 </option>
<option value="2"> 2 </option>
</select>
</td><td id="c1">
12.50 Euro
</td><td id="d1"><span></span>
</td></tr>
</table>
</form>
</body>
Good Luck, skriptor
LuigiX
11-14-2003, 01:10 PM
Hi Scriptor
That's exactly what I was after. Moreover the code is clean and flexible - wonderful!
Just a couple of refinements if possible.
Can you insert a "running total" field in the totals column that calculates the total of all the items on the fly.
Can you express all values in currency format ie $xx.xx
Many thanks again
Luigi