Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Feb 2006
    Posts
    62
    Thanks
    1
    Thanked 0 Times in 0 Posts

    JS Adding Numbers and Displaying Total

    I have a form with 2 radio boxes and a checkbox. The values of each are numbers. I would like to be able to select 1 of the 2 radio boxes and add the number to the number in the checkbox if it is checked:

    <form>
    <input type="radio" name="conferenceRate" value="250"> Three-day Conference Package - Member
    <input type="radio" name="conferenceRate" value="296"> Three-day Conference Package - Non-member rate

    <input type="checkbox" name="mealRate" value="180">Attendant Meal package
    </form>

    The resulting total would be passed to the page and displayed, and also passed to amount field of a Paypal button.

    Any help on this would be greatly appreciated

  • #2
    Regular Coder mjlorbet's Avatar
    Join Date
    Jan 2008
    Location
    Milwaukee, WI
    Posts
    724
    Thanks
    8
    Thanked 96 Times in 95 Posts
    Code:
     
    function doAdd(){
        return document.getElementsByName("conferenceRate")[0].checked?document.getElementsByName("conferenceRate")[0].value:document.getElementsByName("conferenceRate")[1].value + document.getElementsByName("mealRate")[0].checked?document.getElementsByName("mealRate")[0].value:0;
    }
    window.onload = function(){document.form.mysubmitbutton.onclick = function(){
    document.getElementsByName("hdnField")[0].value = doAdd();};};
    }
    would get added inside script tags in the head element of your document

    Code:
     
    <input type="hidden" name="hdnField" value="" />
    <input type="submit" id="mysubmitbutton" />
    would get added inside your form tags, all that's left to do is set the method and action on your form to the next page. you'll have all the information ready to be extracted, but the way to do it is usually through a server side language.
    -Mike
    "Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender

  • #3
    New Coder
    Join Date
    Feb 2006
    Posts
    62
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Many thanks for the help, but I couldn't get it to work:

    http://www.worldfest.net/ihss/register.asp

    Steve

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Code:
    <form name = "myForm">
    <input type="radio" name="conferenceRate" value="250"> Three-day Conference Package - Member <br>
    <input type="radio" name="conferenceRate" value="296"> Three-day Conference Package - Non-member rate<br><br>
    <input type="checkbox" name="mealRate" value="180">Attendant Meal package<br><br>
    <input type = "button" value = "Calculate Total Cost" onclick = "calc()">
    Your Total Cost  <input type = "text" name = "result">
    </form>
    
    <script type = "text/javascript">
    
    function calc() {
    var total = 0;
    if (document.myForm.conferenceRate[0].checked) {total = total + 250}
    if (document.myForm.conferenceRate[1].checked) {total = total + 296}
    if (document.myForm.mealRate.checked) {total = total + 180}
    document.myForm.result.value = total;
    }
    
    </script>
    If you wish to pass the result to another (different) page then you can best use a cookie.
    Last edited by Philip M; 05-01-2008 at 06:08 PM.


  •  

    Posting Permissions

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