0

I have created a html/css website with different pages, it is a menu and I would like to add checkboxes which add up the total of what they wish to order. As all other aspects of the menu are separate I would like this to be separate so I have created another page and assigned values to each check box and the total works when its on the same page nut not when on different, is there any way to overcome this?

This is the code:

<script type="text/javascript">
    function checkTotal() {
        document.listForm.total.value = '';
        var sum = 0;
        for (i=0;i<document.listForm.choice.length;i++) {
          if (document.listForm.choice[i].checked) {
            sum = sum + parseInt(document.listForm.choice[i].value);
          }
        }
        document.listForm.total.value = sum;
    }
</script>

<input type="checkbox" name="choice" value="8.99" onchange="checkTotal()"/>&pound;8.99<br/>

on another page

 <p>The Total Of The Order is: <input type="text" size="2" name="total" value="0"/>

Please help and if it isn't obvious I am a beginner

trezremay
  • 33
  • 5
  • Make sure you do the actual price calculation server-side. You don't want someone messing with client-side data and ordering something for a much lower price. – Halcyon Jan 10 '15 at 12:48

1 Answers1

0

I would use a cookie. The following code from How do I set/unset cookie with jQuery?.

function createCookie(name, value, days) {
var expires;

if (days) {
    var date = new Date();
    date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
    expires = "; expires=" + date.toGMTString();
} else {
    expires = "";
}
    document.cookie = escape(name) + "=" + escape(value) + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = escape(name) + "=";
    var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0) === ' ') c = c.substring(1, c.length);
    if (c.indexOf(nameEQ) === 0) return unescape(c.substring(nameEQ.length, c.length));
}
return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

Then in your code something like the following

    function checkTotal() {
        erase("checkBoxes");
        var sum = 0;
        for (i=0;i<parseInt(readCookie("checkBoxes"));i++) {
           if (document.listForm.choice[i].checked) {
           sum = sum + parseInt(document.listForm.choice[i].value);
        }
    }
    createCookie("checkBoxes",sum,5);
}

Community
  • 1
  • 1
Rajdeep Dosanjh
  • 1,107
  • 1
  • 8
  • 20