suess0r Posted January 30, 2007 Share Posted January 30, 2007 Hi there,I have a site with my advertisement packages listed on them, with checkboxes as values (found at: http://www.clb411.com/adv.php) When they click on one of the packages I would like it if the subtotal text field will be filled with the sum of the packages onClick of the checkboxes. I would assume I would fill the $textamount with an array that updates onclick somehow but i'm not sure exactly the best way to go about this... can anyone point me in a better direction? Quote Link to comment https://forums.phpfreaks.com/topic/36276-calculating-my-check-box-variables-to-sum-a-total/ Share on other sites More sharing options...
suess0r Posted January 30, 2007 Author Share Posted January 30, 2007 nevermind i solved it myself... here's the code if anyone was interested[quote]<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><form name="listForm"><input type="checkbox" name="choice" value="2" onchange="checkTotal()"/>2<br/><input type="checkbox" name="choice" value="5" onchange="checkTotal()"/>5<br/><input type="checkbox" name="choice" value="10" onchange="checkTotal()"/>10<br/><input type="checkbox" name="choice" value="20" onchange="checkTotal()"/>20<br/>Total: <input type="text" size="2" name="total" value="0"/></form>[/quote] Quote Link to comment https://forums.phpfreaks.com/topic/36276-calculating-my-check-box-variables-to-sum-a-total/#findComment-172479 Share on other sites More sharing options...
bibby Posted January 30, 2007 Share Posted January 30, 2007 Am I right that checkboxes don't carry values?They are either on or off.You may be able to use the rare [b]attr[/b] though. Quote Link to comment https://forums.phpfreaks.com/topic/36276-calculating-my-check-box-variables-to-sum-a-total/#findComment-172594 Share on other sites More sharing options...
suess0r Posted January 30, 2007 Author Share Posted January 30, 2007 No i'm sorry, check boxes can in fact carry values set to them, for example...[quote]<input type="checkbox" name="choice" [b]value="2"[/b] onchange="sumfunction()"/>2[/quote] Quote Link to comment https://forums.phpfreaks.com/topic/36276-calculating-my-check-box-variables-to-sum-a-total/#findComment-172782 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.