...

View Full Version : Maths Script (Help)



Sillyoldduffer
09-29-2012, 07:24 PM
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.

Philip M
09-29-2012, 07:37 PM
<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?"

Sillyoldduffer
09-29-2012, 11:04 PM
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

Philip M
09-30-2012, 10:34 AM
You really ought to be able to make that alteration yourself!



<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>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum