tags:

views:

115

answers:

3

Hi everyone, I have the following code. I need to see how many checkboxes have been checked in my form and if there are 4 or less submit the form and if there are more display an error and don't submit.

function SetHiddenFieldValue() 
{
    var checks = document.getElementById('toppings').getElementsByTagName('input');
    var toppings = new Array();
    var randomNumber = Math.floor((Math.random() * 9000) + 100);
    var totalChecked = 0;
    var itemPrice = 5.99;

    for (i = 0; i < checks.length; i++)
    {
        if (checks[i].checked)
        {
            toppings[i] = checks[i].value;
            totalChecked += 1; 
        }
    }

    if (totalChecked > 4) {
        alert("You can only choose up to Max of 4 Toppings");   
    } else {    
        itemPrice = itemPrice + (totalChecked * 0.99);  
        document.getElementById('my-item-name').value = toppings.join("\t");
        document.getElementById('my-item-id').value = randomNumber;
        document.getElementById('my-item-price').value = itemPrice;
    }

And my form is:

<form id="pizza" name="pizza" method="post" action="" class="jcart" onsubmit="return SetHiddenFieldValue();">
    <input type="hidden" name="my-item-id" id="my-item-id" value="" />
    <input type="hidden" name="my-item-name" id="my-item-name" value="" />
    <input type="hidden" name="my-item-price" id="my-item-price" value="" />
    <input type="hidden" name="my-item-qty" value="1" />
    <input type="submit" name="my-add-button" value=" add " />
</form>

any help would be appreciated.

+2  A: 

Your function needs to return false to cancel the submission or true to proceed. That's why your onsubmit form attribute is set to "return XXX()" rather than just "XXX()"

Vinay Sajip
+3  A: 

Your javascript looks correct. I think the only you should need to do is return false; on your if (totalChecked > 4) statement.

Edit: Here's the section of your javascript function to modify:

if (totalChecked > 4) {
    alert("You can only choose up to Max of 4 Toppings");   
    return false;
} else {    
    itemPrice = itemPrice + (totalChecked * 0.99);  
    document.getElementById('my-item-name').value = toppings.join("\t");
    document.getElementById('my-item-id').value = randomNumber;
    document.getElementById('my-item-price').value = itemPrice;
    return true;
}
CAbbott
Hi, thanks for advice, but where should the return false be?I tried to use it but it doesn't work.
+1  A: 

As mentioned, you should return false from your check function to cancel the submit, and true if the check is okay.

How about something like this?

    <html>
 <head>
  <title>Pizza Order Form</title>
 </head>
 <body>
  <script>
   function SetHiddenFieldValue() {
    var totalChecked = 0;
    var itemPrice = 5.99;
    var toppings = new Array();
    var randomNumber = Math.floor((Math.random() * 9000) + 100);
    var checks = document.getElementsByName('topping');

    for (i = 0; i < checks.length; i++) {
     if (checks[i].checked) {
      toppings[i] = checks[i].value;
      totalChecked += 1;
     }
    }

    if (totalChecked > 4) {
     alert("You can only choose up to Max of 4 Toppings");
     return false;
    }
    else {
     itemPrice = itemPrice + (totalChecked * 0.99);
     document.getElementById('my-item-name').value = toppings.join("\t");
     document.getElementById('my-item-id').value = randomNumber;
     document.getElementById('my-item-price').value = itemPrice;
     return true;
    }
   }
  </script>
  <form id="pizza" name="pizza" method="post" action="" class="jcart" onsubmit="return SetHiddenFieldValue();">
   <input type="hidden" name="my-item-id" id="my-item-id" value="" />
   <input type="hidden" name="my-item-name" id="my-item-name" value="" />
   <input type="hidden" name="my-item-price" id="my-item-price" value="" />
   <input type="hidden" name="my-item-qty" value="1" />
   <div id="toppings">
    <input type="checkbox" name="topping" id="pepperoni" />Pepperoni
    <input type="checkbox" name="topping" id="tomato" />Tomato
    <input type="checkbox" name="topping" id="mushrooms" />Mushrooms
    <input type="checkbox" name="topping" id="peppers" />Peppers
    <input type="checkbox" name="topping" id="olives" />Olives
   </div>
   <input type="submit" name="my-add-button" value=" Add " />
  </form>
 </body>
</html>
Synetech inc.