Results 1 to 3 of 3

Thread: Calculated Hidden Field

  1. #1
    Join Date
    Sep 2005
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Calculated Hidden Field

    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.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    HTML Code:
    <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>
    Last edited by Twey; 09-02-2005 at 02:19 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by FormRouter
    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •