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 to the CF scene
    Join Date
    Sep 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Maths Script (Help)

    Hi, Apoligise if I have posted in the incorrect section of the forum. What I am looking for is some help to achieve the following. I am looking for a maths script so that when the user says selects from a form with say ten checkbox's each one having a price to it and lets say they select 5 of the 10and then press a button called add or similar the total will be shown. I did find a simple script in javascript but lost the url I also believe that this would be better done in PHP rather than javascript but any help would be helpful.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Code:
    <form id = "myform">
    5 <input type = "checkbox" name= "chk1" value = "5" >
    15 <input type = "checkbox" name= "chk1" value = "15">
    25 <input type = "checkbox" name= "chk1" value = "25" >
    35 <input type = "checkbox" name= "chk1" value = "35" >
    50 <input type = "checkbox" name= "chk1" value = "50" >
    
    <input type = "button" value = "Get Total" onclick = "addemup()">
    </form>
    
    <script type = "text/javascript">
    
    function addemup() {
    var total = 0;
    var count = 0;
    var f = document.getElementById("myform");
    for (var i=0; i<f.chk1.length; i++) {
    if (f.chk1[i].checked==true) {
    total = total + (f.chk1[i].value*1);
    count++;
    }
    }
    
    if (count == 1) {
    alert ("The value of the checkbox you have checked is " + total);
    }
    else if (count>1) {
    alert ("The total of the " + count + " checkboxes you have checked is " + total);
    }
    }
    
    </script>
    Tourist in hotel in Jasper, Canada: "Do we need to convert our Canadian currency into British pounds when we go on to British Columbia?"
    Last edited by Philip M; 09-29-2012 at 07:43 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    Sillyoldduffer (09-29-2012)

  • #3
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    WOw...Thank you for the "Speedy" answer this is what I was looking for...just another little query is it possible to have the answer displyed in the form instead of a pop up window....once angain thank for answering so fast to a noobie

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    You really ought to be able to make that alteration yourself!

    Code:
    <form id = "myform">
    5 <input type = "checkbox" name= "chk1" value = "5" >
    15 <input type = "checkbox" name= "chk1" value = "15">
    25 <input type = "checkbox" name= "chk1" value = "25" >
    35 <input type = "checkbox" name= "chk1" value = "35" >
    50 <input type = "checkbox" name= "chk1" value = "50" >
    
    <input type = "button" value = "Get Total" onclick = "addemup()"><br><br>
    <span id = "msg"></span>
    
    </form>
    
    <script type = "text/javascript">
    
    function addemup() {
    var total = 0;
    var count = 0;
    var f = document.getElementById("myform");
    for (var i=0; i<f.chk1.length; i++) {
    if (f.chk1[i].checked==true) {
    total = total + (f.chk1[i].value*1);
    count++;
    }
    }
    
    if (count == 1) {
    var message = "The value of the checkbox you have checked is " + total;
    }
    else if (count>1) {
    var message = "The total of the " + count + " checkboxes you have checked is " + total;
    }
    document.getElementById("msg").innerHTML = message;
    
    }
    
    </script>

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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