Page 1 of 1

[solved] Formbuilder: Calculating Total from checked Checkboxes

Posted: Mon May 24, 2010 4:06 pm
by jchelpdeskwv
Good day everyone,

I would appreciates suggestions for the following:  I have a form where clients can select the product that they want (checkbox).  Products have a cost.  I am trying to display the total of the selected checkboxes.  

Here is what I have tried so far:  Under value when checked I placed the price of the item.  However that messed up my Confirmation page as it replaced the names of the checkboxes with the price.

Any other suggestions?




----------------------------------------------

Cms Version: 1.7

Installed Modules:

   * CMSMailer: 1.73.14
   * FileManager: 1.0.2
   * MenuManager: 1.6.2
   * ModuleManager: 1.3.3
   * News: 2.10.4
   * nuSOAP: 1.0.1
   * Printing: 1.0.4
   * Search: 1.6.2
   * ThemeManager: 1.1.1
   * TinyMCE: 2.7.0
   * CTLModuleMaker: 1.8.9.3
   * CGExtensions: 1.18.7
   * SiteMapMadeSimple: 1.2.1
   * CGExtensions: 1.18.7
   * XContent: initial release - BETA 3
   * Gallery: 1.3
   * Bookmarks: 2.0.1
   * Statistics: 1.0.1
   * Questions: 1.0.3
   * FormBuilder: 0.6.2
   * Glossary: 0.6.8
   * CGSimpleSmarty: 1.4.4
   * CGGoogleMaps: 1.4.1
   * ModuleMaker: 0.3.1
   * phpids: 1.4.7
   * CGCalendar: 1.5.2
   * FrontEndUsers: 1.8.2
   * UserDirectory: 1.0-svn
   * ModuleXtender: 1.0.0
   * NFS: 1.0


Config Information:

   * php_memory_limit:
   * process_whole_template: false
   * max_upload_size: 128000000
   * default_upload_permission: 664
   * assume_mod_rewrite: true
   * page_extension: .html
   * internal_pretty_urls: false
   * use_hierarchy: true


Php Information:

   * phpversion: 5.3.1
   * md5_function: On (True)
   * gd_version: 2
   * tempnam_function: On (True)
   * magic_quotes_runtime: Off (False)
   * E_STRICT: 0
   * E_DEPRECATED: 0
   * memory_limit: 128M
   * max_execution_time: 60
   * safe_mode: Off (False)
   * session_save_path: C:\xampp\tmp (0777)
   * session_use_cookies: On (True)


Server Information:

   * Server Api: apache2handler
   * Server Db Type: MySQL (mysql)
   * Server Db Version: 5.1.41


----------------------------------------------

Re: Formbuilder: Calculating Total from checked Checkboxes

Posted: Mon May 24, 2010 5:02 pm
by Rolf
Hi jchelpdeskwv ,

You could use a dropdown instead of checkboxes...

Yes = 10 Dollar
No = 0 Dollar

or

None = 0 Dollar
1 = 10 Dollar
2 = 20 Dollar
etc.

Hope this helps

Rolf

Re: Formbuilder: Calculating Total from checked Checkboxes

Posted: Mon May 24, 2010 7:11 pm
by jchelpdeskwv
Thank you Rolf for the suggestion.

While I can't use it in this particular situation, I will be using your suggestion in another section of my form.

In this scenario, the client can select layers of a map (checkboxes) and each layer adds to the final cost.

Re: Formbuilder: Calculating Total from checked Checkboxes

Posted: Mon May 24, 2010 7:43 pm
by jchelpdeskwv
If I am able to gather the number of checked checkboxes in the checkbox group (try saying that 20 times), I will be able to run a script on the page to calculate the total that the client has to pay.

Any suggestions on how to get the total of checked checkboxes?  I can do this on a regular form, but working with Formbuilder has me in a blank.

Re: Formbuilder: Calculating Total from checked Checkboxes

Posted: Thu May 27, 2010 9:37 am
by RytoEX
If you're going to run a script upon form submission, you could use the "Form Submission Javascript:" option in FormBuilder.  If a checkbox is checked its "checked" property will equal "true".  I have included a very simple example below.  Of course, if you're using decimal values, use parseFloat() instead.

Code: Select all

<__html>
<head>
<__script__ type="text/javascript">
function getChecked()
{
var checkboxes = document.getElementsByTagName("input");

var total = 0;
for (var i = 0; i < checkboxes.length; i++)
{
  if (checkboxes[i].type == "checkbox" && checkboxes[i].checked === true)
  {
    total += parseInt(checkboxes[i].value);
  }
}

alert(total);
}
</__script>
</head>
</__body>

<form>
<p><input type="checkbox" name="One" value="1">One</p>
<p><input type="checkbox" name="Two" value="2">Two</p>
<p><input type="checkbox" name="Three" value="3">Three</p>

<input type="button" onclick="getChecked()" value="How many checked boxes?">
</form>

<__body>
</__html>

Re: [solved] Formbuilder: Calculating Total from checked Checkboxes

Posted: Wed Jun 09, 2010 2:34 pm
by jchelpdeskwv
Thank you very much for your suggestion. 

I found out that if you create an equation that is longer than the box edge, it will truncate it and you get an error.

Re: [solved] Formbuilder: Calculating Total from checked Checkboxes

Posted: Thu Jun 10, 2010 3:15 am
by RytoEX
jchelpdeskwv wrote: Thank you very much for your suggestion.  

I found out that if you create an equation that is longer than the box edge, it will truncate it and you get an error.
I'm not sure that I understand what you're saying here.  Could you explain more clearly what happened?  What error did you get?


EDIT:
I've modified the example I gave previously to be more CMSMS specific.  See below.



If you are using FormBuilder...
You should use a Check Box Group.  Either specify 0 for "Value when not checked" or check the "Don't submit values for unchecked boxes" option.



If you want to use a Javascript...
If you use the {metadata} tag in your template, enter the Javascript you want to use in the "Page Specific Metadata" box surrounded by {literal} tags.  Example:

Code: Select all

{literal}
<__script__ type="text/javascript">
function getChecked()
{
var checkboxes = document.getElementsByTagName("input");

var total = 0;
for (var i = 0; i < checkboxes.length; i++)
{
  if (checkboxes[i].type == "checkbox" && checkboxes[i].checked === true)
  {
    total += parseInt(checkboxes[i].value);
  }
}

alert(total);
}
</__script>
{/literal}
If you don't use the {metadata} tag in your template, you can insert the Javascript into the template or page using a UDT.



If you want to pass the checkbox values to another page...
To find the name of the checkbox group, check the Field Id from FormBuilder.  If you have a Field Id of "294", your checkbox group name will probably be "cntnt01fbrp__294[]".  Alternatively, you could build the form, view it in a page, and then grab the checkbox group name from there.

Create a UDT to capture the checkbox values.  I've called mine "eval_checkboxes".

Code: Select all

$layers = $_POST['cntnt01fbrp__294'];
$total = 0;
for ($i = 0; $i < count($layers); $i++)
{
  $total += $layers[$i];
}
echo "Checkboxes total: " . $total;

global $gCms;
$smarty = &$gCms->GetSmarty();
$smarty->assign('total', $total); // make variable "total" visible for CMSms

My example echoes the checkboxes' total value and assigns it to the CMSMS/Smarty variable $total, which you can access from within CMSMS using {$total}.

Set a disposition in FormBuilder for "*Call A User Defined Tag With the Form Results" and specify the UDT you just created, eval_checkboxes.  Now you should be able to access the variable {$total} from the Submission template or elsewhere in CMSMS.



Feel free to let me know if you have more questions.  If something went wrong with CMSMS truncating your input, then perhaps you could file a Bug or Feature Request.