PDA

View Full Version : Calculated Hidden Field



FormRouter
09-02-2005, 01:17 PM
I need help trying to develop a calculated field.
I have a form with 5 check boxes, each with a different value. And a text field where a user can enter any value. I need a hidden field that will SUM these 6 items all named "giftAmount".
I have been searching for a way to calculate this and have been unsuccessful. Any assistance to someone learning would be helpful.

Twey
09-02-2005, 02:15 PM
<script type="text/javascript">
var e = document.forms[0].elements;
function calc() {
var total = 0,
t = document.getElementById("total");
for(var i=0;i<e.length;i++) {
if(e[i].type.toLowerCase() === "text") total += parseFloat(e[i].value);
else if(e[i].type.toLowerCase() === "checkbox") total += e[i].value;
}
t.value = total;
}
for(var i=0;i<e.length;i++) e[i].onchange = calc;
</script>
<form>
<input type="checkbox" value="5"/>
<input type="checkbox" value="4"/>
<input type="checkbox" value="3"/>
<input type="checkbox" value="2"/>
<input type="checkbox" value="1"/><br/>
<input type="text" size="2"/>
<input type="hidden" id="total" value="0"/>
</form>

mwinter
09-02-2005, 02:32 PM
I need help trying to develop a calculated field.For what purpose exactly? Client-side calculated fields are only useful when displaying advisory information to the user. The fact that you're using a hidden field suggests that your going to submit that value and do something with it, which is very inadvisable; any unscrupulous user can fiddle the value.

For a worst case example, some online companies were defrauded because they assumed that client-side calculated data could be used in their orders. Some users just sent amounts of one cent for entire computer systems worth hundreds of dollars.


What you want can be done. I just want to make sure you're aware of reasons not to do it.

Mike