Forums

Trying to calculate radio and checkboxes, static totals

revive 16 Dec, 2008
Hey Gang (Bob I need your help!!😀)

I have searched high and low, found mult. scripts online that calc radio buttons or checkboxes.. the one I found that does both requires code in the <body> tag which I just don't want to add to the index.php of the template... less is more.. and more conflicts later is no good. I have one form, with radio and check boxes, all static values of whole numbers (i.e. value="3") and just need a total at the end of the form, automatically calculated without the need for a 'click' of a button.. form will have a submit button to send me the form data.. which includes the radio and checkbox selections.. would also like to include this 'total' into the email as well..

Any ideas?? I am lost.. and by no means a js expert.. not even a newbie yet. Guess today is a good day to start..

Here is one code I found for use on the check boxes, I like it because it's small.. but I don't know if it'll work on the radio buttons, anyone??

<html>
<head>

<script type="text/javascript" language="JavaScript">
function TotalCheckedValues() {
var total = 0;
if(document.F2.a.checked == true) { total += parseFloat(document.F2.a.value); }
if(document.F2.b.checked == true) { total += parseFloat(document.F2.b.value); }
if(document.F2.c.checked == true) { total += parseFloat(document.F2.c.value); }
if(document.F2.d.checked == true) { total += parseFloat(document.F2.d.value); }
var ts = new String(total);
if(ts.indexOf('.') < 0) { ts += '.00'; }
if(ts.indexOf('.') == (ts.length - 2)) { ts += '0'; }
document.F2.T.value = ts;
}
</script>


</head>
<body>




<form name="F2">
Which do you want?<br>
<input type="checkbox" name="a" value="10"   onClick="TotalCheckedValues()">Item 1 (10.00)        <br>
<input type="checkbox" name="b" value="0"    onClick="TotalCheckedValues()">Item 2 (Free)      <br>
<input type="checkbox" name="c" value="55"   onClick="TotalCheckedValues()">Item 3 (55.00)    <br>
<input type="checkbox" name="d" value="4" onClick="TotalCheckedValues()">Item 4 (4) <br>
Total: <input type="text" name="T" readonly size="5"> <br>
<input type="button" value="Click" onClick="TotalCheckedValues()">
</form>


</body>
</html>


Any help is greatly appreciated, thanks!!


Also, in case someone is curious to the other code I found that validates both radio and checkboxes (but again calls for a separate js file and code in the body tag to work) it's here:
http://javascript.internet.com/forms/update-order-form-no-page-refresh.html
And listed below for the lazy😛


<!-- Paste this code into an external JavaScript file  -->

/* This script and many more are available free online at
The JavaScript Source :: http://javascript.internet.com
Created by: Kevin Hartig :: http://www.grafikfx.net/ */

// Calculate the total for items in the form which are selected.
function calculateTotal(inputItem) {
  with (inputItem.form) {
    // Process each of the different input types in the form.
    if (inputItem.type == "radio") {   // Process radio buttons.
      // Subtract the previously selected radio button value from the total.
      calculatedTotal.value = eval(calculatedTotal.value) - eval(previouslySelectedRadioButton.value);
      // Save the current radio selection value.
      previouslySelectedRadioButton.value = eval(inputItem.value);
      // Add the current radio button selection value to the total.
      calculatedTotal.value = eval(calculatedTotal.value) + eval(inputItem.value);
    } else {   // Process check boxes.
      if (inputItem.checked == false) {   // Item was uncheck. Subtract item value from total.
          calculatedTotal.value = eval(calculatedTotal.value) - eval(inputItem.value);
      } else {   // Item was checked. Add the item value to the total.
          calculatedTotal.value = eval(calculatedTotal.value) + eval(inputItem.value);
      }
    }

    // Total value should never be less than 0.
    if (calculatedTotal.value < 0) {
      InitForm();
    }

    // Return total value.
    return(formatCurrency(calculatedTotal.value));
  }
}

// Format a value as currency.
function formatCurrency(num) {
  num = num.toString().replace(/\$|\,/g,'');
  if(isNaN(num))
     num = "0";
  sign = (num == (num = Math.abs(num)));
  num = Math.floor(num*100+0.50000000001);
  cents = num%100;
  num = Math.floor(num/100).toString();
  if(cents<10)
      cents = "0" + cents;
  for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
      num = num.substring(0,num.length-(4*i+3)) + ',' + num.substring(num.length-(4*i+3));
  return (((sign)?'':'-') + '$' + num + '.' + cents);
}

// This function initialzes all the form elements to default values.
function InitForm() {
  // Reset values on form.
  document.selectionForm.total.value='$0';
  document.selectionForm.calculatedTotal.value=0;
  document.selectionForm.previouslySelectedRadioButton.value=0;

  // Set all checkboxes and radio buttons on form to unchecked.
  for (i=0; i < document.selectionForm.elements.length; i++) {
    if (document.selectionForm.elements[i].type == 'checkbox' | document.selectionForm.elements[i].type == 'radio') {
      document.selectionForm.elements[i].checked = false;
    }
  }
}



<!-- Paste this code into the HEAD section of your HTML document
     Change the file name and path to match the one you created  -->

<script type="text/javascript" src="yourFileName.js"></script>


<!-- Paste this code into the BODY tag -->

<body onload="InitForm();" onreset="InitForm();">


<!-- Paste this code into the BODY section of your HTML document  -->

<div align="center">
<table><tr><td>
  <form method="POST" name="selectionForm">
    <b>Pizza Order</b><br>
    Small $10.00
    <input type="checkbox" name="Steak"   value="10.00"  onclick="this.form.total.value=calculateTotal(this);">  
    Medium $12.00
    <input type="checkbox" name="Chicken" value="12.00" onclick="this.form.total.value=calculateTotal(this);">  
    Large $15.00
    <input type="checkbox" name="Sushi"   value="15.00"  onclick="this.form.total.value=calculateTotal(this);">
    <br><br>
    <b>Extra Toppings (only one selection allowed):</b> <br>
      <input type="radio" name="Sauce" value="0.00" onclick="this.form.total.value=calculateTotal(this);"> None
    <br>
      <input name="Sauce" type="radio"  value="1.00" onclick="this.form.total.value=calculateTotal(this);"> Extra Cheese $1.00
    <br>
      <input type="radio" name="Sauce" value="2.00" onclick="this.form.total.value=calculateTotal(this);"> Vegetarian $2.00
    <br>
      <input type="radio" name="Sauce" value="3.00" onclick="this.form.total.value=calculateTotal(this);"> Meat $3.00
    <br>
    <br>
    <input type="hidden" name="calculatedTotal" value="0">
    <input type="hidden" name="previouslySelectedRadioButton" value="0">
    <strong>Your total is:</strong>
    <input type="text" name="total" readonly onfocus="this.blur();">
  </form>
</td></tr></table>
</div>
<p><div align="center">
<font face="arial, helvetica" size"-2">Free JavaScripts provided<br>
by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
</div><p>


GreyHead 16 Dec, 2008
Hi revive,

I did a simple one of these this morning - may give you some clues
function check() 
{
	var answers = $$('input');
	var score = 0;
	for ( var i = 0; i < answers.length; i++ ) {
		if ( answers[i].checked == true ) {
			score = score + Number(answers[i].value);
		}
	}
        // somefield.value = score;
}
This form only had radio buttons so I've just added up all the values, you probably need to discrimate a bit more - but a class should do it. Also the last line is pseudocode. Set the value of some form input - possibly a hidden field so that it is reported back in $_POST.

I can do a bit more if you post your Form HTML but not for a while - deadlines to meet.

Bob

PS This uses some MooTools code so needs MooTools loaded (which it usually is in Joomla 1.5).
revive 16 Dec, 2008
Hey Bob,

You rock.. fast reply!

I don't really know where to start with your code, as I am a newb to js,.. but I was able to solve half my problem - I needed to calc two sets of fields, one is just a group of 3 radio buttons, so I used this:

<SCRIPT LANGUAGE="JavaScript">
	function setupvalue() 
	{
		for (i=0;i<document.ChronoContact_order.radio50.length;i++) 
		{ 
			if (document.ChronoContact_order.radio50[i].checked) 
			{ 
				radio50_value = document.ChronoContact_order.radio50[i].value; 
			} 
		} 
	document.ChronoContact_order.radio50txt.value=radio50_value;
	}
	</SCRIPT>


Along with this (place in a table row):
<div style="text-align:center;">
                                      <input type="text" name="radio50txt">
                                    </div>


And it displayed the value of the selected radiobox, of that group... Now,.. on to what I started the post on ( I was posting one issue in hopes that I could 'dumb' my way through using one solution to fit both needs)

I have a group of radio buttons (3 buttons) that I then need to add the value of 5 check boxes and display the total in a text field as I did with the first group of radio buttons...

Here is my form code:

<!-- this is the group that I have calculating correctly using the js snippet above in this post -all set for this group (initial cost) -->
<tr class="column1">
  <td class="column1"></td>
  <td><div style="text-align:center;">
      <input value="$499" class="radio" id="Silver" name="radio50" type="radio" onClick="setupvalue()">
      <label for="Silver" class="radio_label">Silver</label>
    </div></td>
  <td><div style="text-align:center;">
      <input value="$799" class="radio" id="Gold" name="radio50" type="radio" onClick="setupvalue()">
      <label for="Gold" class="radio_label">Gold</label>
    </div></td>
  <td><div style="text-align:center;">
      <input value="$1099" class="radio" id="Platinum" name="radio50" type="radio" onClick="setupvalue()">
      <label for="Platinum" class="radio_label">Platinum</label>
    </div></td>
  <td></td>
</tr>

<tr>
  <td colspan="5" style="height:50px;"></td>
</tr>

<!--these following values are monthly fees that need to be shown in a separate text field  -->

<tr class="column1">
  <td class="column1"></td>
  <td><div style="text-align:center;">
      <input value="$22" class="radio" id="Level1" name="radio51" type="radio">
      <label for="Level1" class="radio_label">Level 1</label>
    </div></td>
  <td><div style="text-align:center;">
      <input value="$33" class="radio" id="Level2" name="radio51" type="radio">
      <label for="Level2" class="radio_label">Level 2</label>
    </div></td>
  <td><div style="text-align:center;">
      <input value="$66" class="radio" id="Level3" name="radio51" type="radio">
      <label for="Level3" class="radio_label">Level 3</label>
    </div></td>
  <td><div style="text-align:center;">
      <input value="$650" class="radio" id="Redline" name="radio51" type="radio">
      <label for="Redline" class="radio_label"><b><span style="color:#cc0000;">RED</span>LINE</b></label>
    </div></td>

<tr>
  <td colspan="5" style="height:50px;"></td>
</tr>

<tr class="column1">
  <td class="column1">DBU</td>
  <td colspan="3"><div  style="color:#333; font-size:13px; text-align:left;"></div></td>
  <td><div style="text-align:center;">
      <input value="$10" class="radio" id="dbu" name="radio52" type="radio">
      <label for="dbu" class="radio_label"><br />
      Only 10/mo.</label>
    </div></td>
</tr>
<tr class="odd">
  <td class="column1">WBU</td>
  <td colspan="3"><div style="color:#333; font-size:13px; text-align:left;"></div></td>
  <td><div style="text-align:center;">
      <input value="$5" class="radio" id="WBU" name="radio52" type="radio">
      <label for="WBU" class="radio_label"><br />
      Only 5/mo.</label>
    </div></td>
</tr>
<tr class="column1">
  <td class="column1">NoBU</td>
  <td colspan="3"><div  style="color:#333; font-size:13px; text-align:left;"></div></td>
  <td><div style="text-align:center;">
      <input value="$0" class="radio" id="NoBU" name="radio52" type="radio">
      <label for="NoBU" class="radio_label"><br />
      - </label>
    </div></td>
</tr>
<tr class="odd">
  <td class="column1">Mgmnt. </td>
  <td colspan="3"><div style="color:#333; font-size:13px;  text-align:left;"></div></td>
  <td><div style="text-align:center;">
      <input value="$33" class="radio" id="MGMNT" name="radio53" type="checkbox">
      <label for="MGMNT" class="radio_label"><br />
      From 30/mo.</label>
    </div></td>
</tr>
<tr class="column1">
  <td class="column1">Other Requirements</td>
  <td colspan="3"><div style="text-align:center; color:#333;"></div></td>
  <td><div style="text-align:center;">
      <input value="$0" class="radio" id="Other" name="radio54" type="checkbox">
      <label for="Other" class="radio_label"><br />
      Personal Quote</label>
    </div></td>
</tr>
<tr class="odd">
  <td class="column1" colspan="5" height="50px"></td>
</tr>

<tr class="column1">
  <td class="column1" colspan="2">rev</td>
  <td><div style="text-align:center;">
      <input value="$3" class="radio" id="Rev" name="radio55" type="checkbox">
      <label for="Rev" class="radio_label"><br />
      Only 3/mo.</label>
    </div></td>
</tr>

<tr class="column1">
  <td class="column1">AWC</td>
 
  <td><div style="text-align:center;">
      <input value="$15" class="radio" id="AWC" name="radio56" type="checkbox">
      <label for="AWC" class="radio_label"><br />
      Only 15/mo.</label>
    </div></td>
</tr>
<tr class="odd">
 
  <td><div style="text-align:center;">
      <input value="$5" class="radio" id="ARC" name="radio57" type="checkbox">
      <label for="ARC" class="radio_label"><br />
      Only 5/mo.</label>
    </div></td>
</tr>
<tr>
  <td colspan="5" height="50px"></h4></td>
</tr>
</tbody>
</table>
</div>
<div  class="form_item">
  <div class="form_element cf_textarea">
    <label class="cf_label"><img src="images/icon_required.gif" /> Are there any other comments/specific ideas?</label>
    <textarea class="cf_inputbox required" rows="10" id="text_49" cols="50" name="text_49"></textarea>
  </div>
  <div class="clear"> </div>
</div>
<div  class="form_item">
  <div class="form_element cf_captcha">
    <label class="cf_label">Verification</label>
    <span>{imageverification}</span><br />
    <span style="font-size:12px;">Not Case Sensitive</span> </div>
  <div class="clear"> </div>
</div>
<tr>
  <td colspan="5"><div style="text-align:center;">INITIAL $:
      <input type="text" size="6" disabled="disabled" name="radio50txt">
    </div></td>
</tr>
<!-- Need another text field here with total of recurring fees -->
<tr>
  <td colspan="5"><div style="text-align:center;">MONTHLY $:
      <input type="text" size="6" disabled="disabled" name="?????">
    </div></td>
</tr>

<div  class="form_item">
  <div class="form_element cf_button">
    <input value="Submit" name="undefined" type="submit">
  </div>
  <div class="clear"> </div>
</div>



ANY help is greatly appreciated!! I can manage my way through the values and naming of the input fields, etc.. but I really don't know js.. all I did with the above code was change the names in it and my form to correlate with each other..😛 See, what I mean by 'dumbing' my way through it? LOL.. I'm trying to learn as fast as I can.. so this really helps!

Thanks again Bob!
revive 16 Dec, 2008
Hey Bob,

Think I found one that might work...
JS code:
<script type="text/javascript">
<!--
function calculateTotal(ChronoContact_webdesign_hosting_order){
var total = 0;
var elems = ChronoContact_webdesign_hosting_order.getElementsByTagName('input');
for(var i = 0; i < elems.length; i++){
if((elems[i].type == "radio" || elems[i].type == "checkbox") && elems[i].checked){
total += parseFloat(elems[i].value);
}
}
total = total.toFixed(2)
ChronoContact_webdesign_hosting_order.total.value = "$" + total;
}
// -->
</script>




FORM CODE:
<form name="ChronoContact_webdesign_hosting_order" method="post">
<strong>How many Sessions will the camper be attending?</strong><br />
<input type="radio" name="Session" value="649.95" onclick="calculateTotal(this.form)" />1 Session $649.95<br />
<input type="radio" name="Session" value="1299.99" onclick="calculateTotal(this.form)" />2 Sessions $1,299.99<br />
<input type="radio" name="Session" value="1950" onclick="calculateTotal(this.form)" />3 Sessions $1,950.00<br />
<input type="radio" name="Session" value="2600" onclick="calculateTotal(this.form)" />4 Sessions $2,600.00<br />
<input type="radio" name="Session" value="3250" onclick="calculateTotal(this.form)" />5 Session $3,250.00

<p><strong>Fee for supervising campers that are staying at camp between Sessions. Please specify how many weekends the campers are expected to stay at camp:</strong><br />
<input type="radio" name="Supervision" value="74.95" onclick="calculateTotal(this.form)" />1 Weekend $74.95<br />
<input type="radio" name="Supervision" value="149.99" onclick="calculateTotal(this.form)" />2 Weekends $149.99<br />
<input type="radio" name="Supervision" value="225" onclick="calculateTotal(this.form)" />3 Weekend $225.00<br />
<input type="radio" name="Supervision" value="300" onclick="calculateTotal(this.form)" />4 Weekend $300.00<br />
<input type="radio" name="Supervision" value="375" onclick="calculateTotal(this.form)" />5 Weekends $375.00
<p><strong>Discounts</strong><br />

<input type="checkbox" name="Discount_one" value="49.99" onclick="calculateTotal(this.form)" />$49.99 Discount   <input type="checkbox" name="Discount_two" value="19.95" onclick="calculateTotal(this.form)" />$19.95 

<p><strong>Your total is:</strong><input type="text" name="total" readonly onfocus="this.blur()" value="$0.00" />
</form>


It seems to work standalone, but how do I add another JS snippet to the JS Code section when there is already one in there to calculate the first group of radio boxes???

When I added this JS snippet below the other one, and ran the form the first group of radio boxes works great.. calcs perfectly.. the second snippet(included above) produces: $NaN when an option is selected... Further testing by removing the first JS piece still caused this error on the second calc..


************** EDIT ******************

Ok, found it.. the second JS which was calculating the values, not just displaying them.. didn't like the '$' that the first JS code was simply displaying..


BUT, now the second JS code adds in the value of the first code.. So, the first code which is just to calc the setup cost, is being added to the second codes total which is used to calc the monthly cost on the form..

So, guess we're on another trail.. hope this one is easier😀

:D
GreyHead 16 Dec, 2008
Hi revive,

Played with this a little. I think this version works Ok and is pretty! Form HTML has a div wrapping the input fields; the discounts have negative values and the result goes both into a span for display and a hidden field for saving.
<div id='radios'>
<p><strong>How many Sessions will the camper be attending?</strong></p>
<input type="radio" name="Session" value="649.95" onclick="calculateTotal(this.form)" />1 Session $649.95<br />
<input type="radio" name="Session" value="1299.99" onclick="calculateTotal(this.form)" />2 Sessions $1,299.99<br />
<input type="radio" name="Session" value="1950" onclick="calculateTotal(this.form)" />3 Sessions $1,950.00<br />
<input type="radio" name="Session" value="2600" onclick="calculateTotal(this.form)" />4 Sessions $2,600.00<br />
<input type="radio" name="Session" value="3250" onclick="calculateTotal(this.form)" />5 Session $3,250.00

<p><strong>Fee for supervising campers that are staying at camp between Sessions. Please specify how many weekends the campers are expected to stay at camp:</strong></p>
<input type="radio" name="Supervision" value="74.95" onclick="calculateTotal(this.form)" />1 Weekend $74.95<br />
<input type="radio" name="Supervision" value="149.99" onclick="calculateTotal(this.form)" />2 Weekends $149.99<br />
<input type="radio" name="Supervision" value="225" onclick="calculateTotal(this.form)" />3 Weekend $225.00<br />
<input type="radio" name="Supervision" value="300" onclick="calculateTotal(this.form)" />4 Weekend $300.00<br />
<input type="radio" name="Supervision" value="375" onclick="calculateTotal(this.form)" />5 Weekends $375.00

<p><strong>Discounts</strong><br />
<input type="checkbox" name="Discount_one" value="-49.99" onclick="calculateTotal(this.form)" />$49.99 Discount   
<input type="checkbox" name="Discount_two" value="-19.95" onclick="calculateTotal(this.form)" />$19.95
</div>

<p><strong>Your total is:</strong><span id='total_s' > </span>
<input type="hidden" name="total_h" id="total_h" value="" />
and the JavaScript just sums everything 'checked' inside the div and writes a formatted version to the span.
function calculateTotal()
{
	var total = 0;
	var radios = $$('div#radios input');
	for ( var i = 0; i < radios.length; i++ ) {
		if ( radios[i].checked ) {
		 total = total + parseFloat(radios[i].value);
		}
	}
	$('total_s').innerHTML = " $"+total.toFixed(2);
	$('total_h').value = total;
}

Bob
revive 16 Dec, 2008
Hey Bob,

Thanks for the reply!

I think I may have gotten ahead of myself with posting all the codes I was looking into.. The one you modified was a code I found and tried to modify myself,.. I got it to work, but don't know how to make it NOT include the values of radio button 'name'"radio50"...

Here is the JS that IS working, but calcs all the buttons value:
<script type="text/javascript">
<!--
function calculateTotal(ChronoContact_webdesign_hosting_order){
var total = 0;
var elems = ChronoContact_webdesign_hosting_order.getElementsByTagName('input');
for(var i = 0; i < elems.length; i++){
if((elems[i].type == "radio" || elems[i].type == "checkbox") && elems[i].checked){
total += parseFloat(elems[i].value);
}
}
total = total.toFixed(2)
ChronoContact_webdesign_hosting_order.total.value = "$" + total;
}


My form code is:


<div id="clubContent">
  <table cellpadding="2" width="100%">
    <tbody>
      <tr>
        <td colspan="5"></td>
      <tr>
        <td class="caption" width="55%"></td>
        <td width="15%"><div style="text-align:center;"><span style="color: #808080;"><b>SILVER</b></span><br />
            <img src="images/silver_package.png" alt="-" align="middle" height="77" width="74" class="png" /></div></td>
        <td width="15%"><div style="text-align:center;"><span style="color: #d2a719;"><b>GOLD</b></span><br />
            <img src="images/gold_package.png" alt="-" align="middle" height="77" width="73" class="png" /></div></td>
        <td width="15%"><div style="text-align:center;"><span style="color: #6a6a98;"><b>PLATINUM</b></span><br />
            <img src="images/platinum_package.png" alt="-" align="middle" height="77" width="74" class="png" /></div></td>
        <td width="15%"> </td>
      </tr>
      
      <tr class="column1">
        <td class="column1"></td>
        <td><div style="text-align:center;">
            <input value="499" class="radio" id="Silver" name="radio50" type="radio" onClick="setupvalue()">
            <label for="Silver" class="radio_label">Silver</label>
          </div></td>
        <td><div style="text-align:center;">
            <input value="799" class="radio" id="Gold" name="radio50" type="radio" onClick="setupvalue()">
            <label for="Gold" class="radio_label">Gold</label>
          </div></td>
        <td><div style="text-align:center;">
            <input value="1099" class="radio" id="Platinum" name="radio50" type="radio" onClick="setupvalue()">
            <label for="Platinum" class="radio_label">Platinum</label>
          </div></td>
        <td></td>
      </tr>
      <tr class="odd">
        <td class="column1"> </td>
        <td><div style="text-align:center;"><img src="images/stories/silverpkg.png" alt="silver pkg" class="png" /></div></td>
        <td><div style="text-align:center;"><img src="images/stories/goldpkg.png" alt="gold pkg" class="png" /></div></td>
        <td><div style="text-align:center;"><img src="images/stories/platinumpkg.png" alt="platinum pkg" class="png" /></div></td>
        <td></td>
      </tr>
      <tr>
        <td colspan="5" style="height:50px;"></td>
      </tr>
      <tr>
        <td colspan="5"><div class="number-step2" style="height:75px; width:225px;"></div>
          <span style="color:#333; font-weight:bold; float:left;">:</span><br />
          <br /></td>
      </tr>
      
      <tr class="odd">
        <td class="column1"> </td>
        <td> </td>
        <td> </td>
        <td> </td>
        <td> </td>
      </tr>
      <tr class="column1">
        <td class="column1">Levels</td>
        <td><div style="text-align:center;">
            <input value="22" class="radio" id="Level1" name="radio51" type="radio" onclick="calculateTotal(this.form)">
            <label for="Level1" class="radio_label">Level 1</label>
          </div></td>
        <td><div style="text-align:center;">
            <input value="33" class="radio" id="Level2" name="radio51" type="radio" onclick="calculateTotal(this.form)">
            <label for="Level2" class="radio_label">Level 2</label>
          </div></td>
        <td><div style="text-align:center;">
            <input value="66" class="radio" id="Level3" name="radio51" type="radio" onclick="calculateTotal(this.form)">
            <label for="Level3" class="radio_label">Level 3</label>
          </div></td>
        <td><div style="text-align:center;">
            <input value="650" class="radio" id="Redline" name="radio51" type="radio" onclick="calculateTotal(this.form)">
            <label for="Redline" class="radio_label"><b><span style="color:#cc0000;">RED</span>LINE</b></label>
          </div></td>
      </tr>
      
    <tr>
      <td colspan="5" style="height:50px;"></td>
    </tr>
    <tr>
      <td colspan="5"><div class="number-step3" style="height:75px; width:225px;"></div>
        <span style="color:#333; font-weight:bold; float:left;">:</span><br />
        <br /></td>
    </tr>
    <tr>
      <td colspan="5" style="height:50px;"></td>
    </tr>
    <tr>
      <td colspan="5"><h4 style="text-align: center;"><img src="images/addons.jpg" alt="-" align="middle" /> <b>ADDONS</b></h4></td>
    </tr>
    <tr class="column1">
      <td class="column1"> </td>
      <td colspan="3"> </td>
      <td><div style="text-align:center;">
          <input value="10" class="radio" id="DailyBackup" name="radio52" type="radio" onclick="calculateTotal(this.form)">
          <label for="DailyBackup" class="radio_label"><br />
          Only 10/mo.</label>
        </div></td>
    </tr>
    <tr class="odd">
      <td class="column1"> </td>
      <td colspan="3"> </td>
      <td><div style="text-align:center;">
          <input value="5" class="radio" id="WeeklyBackup" name="radio52" type="radio" onclick="calculateTotal(this.form)">
          <label for="WeeklyBackup" class="radio_label"><br />
          Only 5/mo.</label>
        </div></td>
    </tr>
    <tr class="column1">
      <td class="column1"> </td>
      <td colspan="3"> </td>
      <td><div style="text-align:center;">
          <input value="0" class="radio" id="NoBackup" name="radio52" type="radio" onclick="calculateTotal(this.form)">
          <label for="NoBackup" class="radio_label"><br />
          - </label>
        </div></td>
    </tr>
    <tr class="odd">
      <td class="column1"> </td>
      <td colspan="3"> </td>
      <td><div style="text-align:center;">
          <input value="30" class="radio" id="ProMgmnt" name="radio53" type="checkbox" onclick="calculateTotal(this.form)">
          <label for="ProMgmnt" class="radio_label"><br />
          From 30/mo.</label>
        </div></td>
    </tr>
    <tr class="column1">
      <td class="column1"> </td>
      <td colspan="3"> </td>
      <td><div style="text-align:center;">
          <input value="0" class="radio" id="Other" name="radio54" type="checkbox" onclick="calculateTotal(this.form)">
          <label for="Other" class="radio_label"><br />
          Personal Quote</label>
        </div></td>
    </tr>
    <tr class="odd">
      <td class="column1" colspan="5" height="50px"></td>
    </tr>
    <tr>
      <td colspan="5"><h4 style="text-align: center;"> </h4></td>
    </tr>
    <tr class="column1">
      <td class="column1" colspan="2"> </td>
      <td> </td>
      <td> </td>
      <td><div style="text-align:center;">
          <input value="3" class="radio" id="RevFiles" name="radio55" type="checkbox" onclick="calculateTotal(this.form)">
          <label for="RevFiles" class="radio_label"><br />
          Only 3/mo.</label>
        </div></td>
    </tr>
    <tr class="odd">
      <td class="column1" colspan="5" height="50px"></td>
    </tr>
    <tr>
      <td colspan="5"><h4 style="text-align: center;"> </h4></td>
    </tr>
    <tr class="odd">
      <td class="column1"></td>
      <td><div style="text-align:center; color:#333;"></div></td>
      <td><div style="text-align:center; color:#333;"></div></td>
      <td><div style="text-align:center; color:#333;"></div></td>
      <td><div style="text-align:center;"></div></td>
    </tr>
    <tr class="column1">
      <td class="column1"> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td><div style="text-align:center;">
          <input value="15" class="radio" id="WebChunk" name="radio56" type="checkbox" onclick="calculateTotal(this.form)">
          <label for="WebChunk" class="radio_label"><br />
          Only 15/mo.</label>
        </div></td>
    </tr>
    <tr class="odd">
      <td class="column1"> </td>
      <td> </td>
      <td> </td>
      <td> </td>
      <td><div style="text-align:center;">
          <input value="5" class="radio" id="RevChunk" name="radio57" type="checkbox" onclick="calculateTotal(this.form)">
          <label for="RevChunk" class="radio_label"><br />
          Only 5/mo.</label>
        </div></td>
    </tr>
    <tr class="column1">
      <td class="column1"> </td>
      <td><div style="text-align:center;"></div></td>
      <td><div style="text-align:center;"></div></td>
      <td><div style="text-align:center;"></div></td>
      <td><div style="text-align:center;"> - </div></td>
    </tr>
    <tr>
      <td colspan="5" height="50px"></h4></td>
    </tr>
    <tr>
      <td><div style="float:right;">YOUR TOTALS ARE:</div></td>
      <td><div style="text-align:center;">DESIGN FEE:
          <input type="text" size="6" disabled="disabled" name="radio50txt" value="$0.00">
        </div></td>
      <td><div style="text-align:center;">MONTLY FEE:
<input type="text" name="total" size="5" readonly onfocus="this.blur()" value="$0.00" />
        </div></td>
      <td></td>
      <td></td>
    </tr>
    </tbody>
    
  </table>
</div>
<div  class="form_item">
  <div class="form_element cf_textarea">
    <label class="cf_label"><img src="images/icon_required.gif" /> Are there any other comments/specific ideas?</label>
    <textarea class="cf_inputbox required" rows="10" id="text_49" cols="50" name="text_49"></textarea>
  </div>
  <div class="clear"> </div>
</div>
<div  class="form_item">
  <div class="form_element cf_captcha">
    <label class="cf_label">Verification</label>
    <span>{imageverification}</span><br />
    <span style="font-size:12px;">Not Case Sensitive</span> </div>
  <div class="clear"> </div>
</div>
<div  class="form_item">
  <div class="form_element cf_button">
    <input value="Submit" name="undefined" type="submit">
  </div>
  <div class="clear"> </div>
</div>



I tried your code, and it looks very clean, but cannot get the value to show.. it's just blank.. ideas??
GreyHead 16 Dec, 2008
Hi revive,

Oops . . . trying to to too much in a rush - I did think it was a strange form for a webhosting order. I'll take a look at the next bunch of code. Meanwhile the last one works for me. Maybe something got lost in translation - here's the form backup.

Bob

[attachment=1]16-12-2008 21-13-23.png[/attachment]

PS I tend to develop these by using 'require_once' clauses in the ChronoForms boxes and putting the code in a folder so that I can edit the files with an FTP editor instead of fiddling around inside the little edit boxes.
revive 17 Dec, 2008
No worries, thanks so much for helping me!!

I tried out your backup of the form and it works great! So, how do I get this accomplished:

I have a js snippet calculating the value for the radio button group named 'radio50' - which are the startup fees.
Now, we have the snippet that calculates the values for ALL radio/checkboxes and gives that total - this should exclude the value for the 'radio50' button group.. and then just show the monthly fees..

Is there a way to exclude that radio50 group from the second codes calcs? Or, is there a better way to group the fields that need calc'ing in the js itself..?
GreyHead 17 Dec, 2008
Hi revive,

Sorry, I cracked this last night - then got into watching a movie and it got to late to come and post it. You need to make a couple of changes to the form - add 'design' classes to the radio50 boxes
<input value="499" class="radio design" id="Silver" name="radio50" type="radio" onClick="calculateTotal()"> 
and, in exactly the same way add 'month' classes to the other inputs. Then add a couple of 'span's to the total display
<div style="text-align: center;">DESIGN FEE: <span id='total_d'> </span>
<div style="text-align: center;">MONTLY FEE: <span id='total_m'> </span>
And here's the JavaScript to go with it
function calculateTotal() 
{
	var design = $$('table .design');
	var total_d = 0;
	for ( var i = 0; i < design.length; i++) {
		if ( design[i].checked ) {
			total_d += parseFloat(design[i].value);
		}
	}
	$('total_d').innerHTML = " $"+total_d.toFixed(2);
	
	var month  = $$('table .month');
	var total_m = 0;
	for ( var i = 0; i < month.length; i++) {
		if ( month[i].checked ) {
			total_m += parseFloat(month[i].value);
		}
	}
 	$('total_m').innerHTML = " $"+total_m.toFixed(2);
}
Note that this repeats identical code twice - once for the design fees and once for the monthly fees. If I had time I'm sure that this could be converted into a neat array + function code. Maybe later.

The one piece that isn't here is to put the values into hidden form inputs - that's in the earlier example if you need it.

Bob
GreyHead 17 Dec, 2008
Hi revive,

Couldn't resist - here's the generic version - will work with any number of input groups distinguished by class and put the results in the corresponding spans
function calculateTotal() 
{
	types = new Array('design', 'month');
	spans = new Array('total_d', 'total_m')
	for ( i = 0; i < types.length; i++ ) {
		$(spans[i]).innerHTML = " $"+sumValues(types[i]).toFixed(2);
	}
}
function sumValues(type)
{	
	var types = $$('table .' + type);
	var total = 0;
	for ( var i = 0; i < types.length; i++) {
		if ( types[i].checked ) {
			total += parseFloat(types[i].value);
		}
	}
	return total;
}

Bob

PS These functions don't use the form name at all so you could be tidy and remove the 'this.form's from the event calls in the html.
revive 17 Dec, 2008
Bob,.. you are amazing! That works beautifully!!!! Now I know why they referred to you as Obi-Wan Kenobi on another post!

I just tested it and the calc works great.. won't calc the first 'group' until you select at least one from the second group,.. but that shouldn't be an issue as the pages flows in a way that you woulnd't even see it until both groups have been selected from.

But, it did, for some reason, kill the validation of fields... no required remain required.. you can tab through them without warnings.. any thoughts??
GreyHead 17 Dec, 2008
Hi revive,

Thank you. I missed the Obi Wan Kenobi post though, I'll go hunt for that ;-)

Off the top of my head I can't think why it would affect the validation, maybe changing the classes doesn't work. I can take a look later. What do you need validated?

Bob
revive 17 Dec, 2008
Hey Bob,..

Thanks so much, these forms are really coming together and will be a great help to our clients.

One question regarding the way the js outputs the total values:

Is there a way to pass the value into a 'readonly' or 'disabled' field instead of a raw span? I like the look of the spans, but instead of then having to create more fields (hidden) to capture the data, I'd rather have them output to something like:

function calculateTotal()
{
   var design = $$('table .design');
   var total_d = 0;
   for ( var i = 0; i < design.length; i++) {
      if ( design[i].checked ) {
         total_d += parseFloat(design[i].value);
      }
   }
   $('total_d').innerHTML = " $"+total_d.toFixed(2);
   
   var month  = $$('table .month');
   var total_m = 0;
   for ( var i = 0; i < month.length; i++) {
      if ( month[i].checked ) {
         total_m += parseFloat(month[i].value);
      }
   }
   $('total_m').innerHTML = " $"+total_m.toFixed(2);
}


Outputs to:

          <input type="text" size="6" disabled="disabled" name="total_d">
          <input type="text" size="6" disabled="disabled" name="total_m">



How would I modify the js to make that happen???

Also, I checked all the settings and the required fields are just not 'required'... no warnings.. weird

Also, and I mean this in all sincerity, how can I, and other members of this forum, say 'Thanks' for the help.. do you have a donation area, or a 'buy me a latte' button somewhere? I may not have tons of cash laying around, but I do appreciate the help you've put in here..
GreyHead 20 Dec, 2008
Hi revive,

Sorry to have been a while replying.

Yes, you can write the result almost anywhere into the page html. To write it into an input field you'll need to set the value (and make sure that the field is locked to prevent tampering).
In the form html
<div>
<label for='total'>Total Design fee $</label>
<input type='text' name='total_d' id='total_d' value='' readonly='readonly' /><br />
<label for='total'>Total montly fee $</label>
<input type='text' name='total_m' id='total_m' value='' readonly='readonly' />
</div>
and in the javascript
 . . .
   for ( i = 0; i < types.length; i++ ) {
      $(spans[i]).value = sumValues(types[i]).toFixed(2);
   }
. . .
NB not tested and may need debugging :-)

BTW don't use 'disabled='disabled' as a disabled field doesn't pass any value back to the $_POST array and your total will not be saved; it is completely disabled and 'for display only'.

do you have a donation area, or a 'buy me a latte' button somewhere?


Thank you for the kind thought which are much appreciated . . . there is a working demo of my PayPal module here (in the righthand column) . . . not sure about beers or lattes but contributions to the next book are always welcome.

Bob
revive 21 Dec, 2008
Hey Bob,
Thanks again for the code.. and I know this makes me look like a 'fresh-off-the-press-es-newbie', but I have tried everything I can think of to get it to work.. no dice.. fields just don't calc.

Here is the orig js script:
function calculateTotal()
{
   var design = $$('table .design');
   var total_d = 0;
   for ( var i = 0; i < design.length; i++) {
      if ( design[i].checked ) {
         total_d += parseFloat(design[i].value);
      }
   }
   $('total_d').innerHTML = " $"+total_d.toFixed(2);
   
   var month  = $$('table .month');
   var total_m = 0;
   for ( var i = 0; i < month.length; i++) {
      if ( month[i].checked ) {
         total_m += parseFloat(month[i].value);
      }
   }
   $('total_m').innerHTML = " $"+total_m.toFixed(2);
}


And the orig spans you had the totals set to show in:

    <tr class="column1">
      <td class="column1" colspan="3"><div style="float:right;"><strong>YOUR TOTALS ARE AS FOLLOWS -  </strong></div></td>
      <td><div style="text-align:left;"><strong>EST. SETUP COST(S):</strong><br /><span id='total_d'> </span></div></td>
      <td><div style="text-align:left;"><strong>EST. COST/MO:</strong><br /><span id='total_m'>&


And here is ONE of the evolutions of this JS script with your change to it:
function calculateTotal()
{
   var design = $$('table .design');
   var total_d = 0;
   for ( i = 0; i < types.length; i++ ) {
      if ( design[i].checked ) {
         total_d += parseFloat(design[i].value);
      }
   }
   $(spans[i]).value = sumValues(types[i]).toFixed(2);
   
   var month  = $$('table .month');
   var total_m = 0;
   for ( i = 0; i < types.length; i++ ) {
      if ( month[i].checked ) {
         total_m += parseFloat(month[i].value);
      }
   }
   $(spans[i]).value = sumValues(types[i]).toFixed(2);
}


and the HTML the data should show in:
<tr class="column1">
      <td class="column1" colspan="3"><div style="float:right;"><strong>YOUR TOTALS ARE AS FOLLOWS -  </strong></div></td>
      <td><div style="text-align:left;"><label for='total'>Est. Setup Cost(s) $</label>
<input type='text' name='total_d' id='total_d' value='' readonly='readonly' /></div></td>
      <td><div style="text-align:left;"><label for='total'>Est. Cost/Mo. $</label>
<input type='text' name='total_m' id='total_m' value='' readonly='readonly' /></div></td>
      </tr>


Got any migraine meds?? LOL..

any help is greatly appreciated!
J
GreyHead 21 Dec, 2008
Hi revive,

I think we dropped a step somewhere along the line. I just tested this combo and it writes to both the span and the input. Javascript:
function calculateTotal() 
{
	types = new Array('design', 'month');
	spans = new Array('total_d', 'total_m');
	inputs = new Array('total_di', 'total_mi');
	for ( i = 0; i < types.length; i++ ) {
		$(spans[i]).innerHTML = " $"+sumValues(types[i]).toFixed(2);
		$(inputs[i]).value = sumValues(types[i]).toFixed(2);
	}
}
function sumValues(type)
{	
	var types = $$('table .' + type);
	var total = 0;
	for ( var i = 0; i < types.length; i++) {
		if ( types[i].checked ) {
			total += parseFloat(types[i].value);
		}
	}
	return total;
}
and htmp (total fields only)
			<td>
			<div style="float: right;">YOUR TOTALS ARE:</div>
			</td>
			<td>
			<div style="text-align: center;">DESIGN FEE: <span id='total_d'> </span>
			<input type="text"
				size="6" id='total_di' readonly="readonly" name="radio50txt" value="$0.00">
			</div>
			</td>
			<td>
			<div style="text-align: center;">MONTHLY FEE: <span id='total_m'> </span>
			<input type="text"
				name="total" id='total_mi' size="5" readonly='readonly' onfocus="this.blur()" value="$0.00" />
			</div>
			</td>

Bob
revive 21 Dec, 2008
Hey Bob,
Yes, you are absolutely correct,.. and this shows what a newb I am to PHP and especially JS.. I had the ks script ALL MIXED up.. works now.. writing to both spans and inputs... now I can pull the spans and fly solo with the input fields, so they will return in my email template, correct??

Thanks!
J
GreyHead 21 Dec, 2008
Hi revive,

Yes surely you can. Make sure that you have them set to 'readonly' though, not disabled.

Bob

PS And personally, I'd make the inputs hidden and pretty-format the spans . . . but its up to you.
revive 22 Dec, 2008
Great idea ! Running with it and it looks great!
This topic is locked and no more replies can be posted.