This forum is no longer open and is for reading/searching only.
Please use our new MachForm Community Forum instead.
MachForm Community Forums » MachForm 2
How to add a field that calculates the sum of other fields ?
Started 15 years ago by Leopard | 5 posts |
-
i need a field which is visible for admin only this field calculates the sum of other fields:
element_4 = element_1 + element_2 + element_3
the field must be generated automatically and can be changed only if the the other fields changed so the sum will be changed
Thanks :)
Posted 15 years ago # -
Any idea ?
Posted 15 years ago # -
Hmmm .. It's possible to use a javascript for calculation and add a "hidden" field for calculation result. To do so edit "includes/view-functions.php" file and try to follow these steps - It's quite long ;) - :
1. Go to line 1818 ~ 1822, you will see these code :
if($has_calendar){ $calendar_js = '<script type="text/javascript" src="js/calendar.js"></script>'; }else{ $calendar_js = ''; }
Put these code exactly bellow that line
if ($_GET['id'] == 3) { $calculate_js = <<<EOT <script type="text/javascript"> function calculate() { //reset text box value $('#element_4').val('0'); sum_data = parseInt($('#element_1').val()) + parseInt($('#element_2').val()) + parseInt($('#element_3').val()) ; $('#element_4').val (sum_data); } </script> EOT; }
2. Go to line 1851 and you will see this code
<script type="text/javascript" src="js/view.js"></script>
put this code exactly bellow that line
<script type="text/javascript" src="js/jquery/jquery-core.js"></script> {$calculate_js}
3. Go to around line 40, you'll see this code :
$element_markup = <<<EOT
put these code above that line
if ($_GET['id'] == 3) { $onchange_script = ' onchange="javascript:calculate();" '; }
4. Go to line 44, you will see this code :
<input id="element_{$element->id}" name="element_{$element->id}" class="element text {$element->size}" type="text" value="{$element->default_value}" />
change to
<input {$onchange_script} id="element_{$element->id}" name="element_{$element->id}" class="element text {$element->size}" type="text" value="{$element->default_value}" />
I there I assume your form id = 3 and the text box id to be calculated are 1,2,3, with field result id = 4.
Anyway for the result, I suggest to use a hidden field instead an admin only field, to hide the result field (element_4), you can add this CSS code to your CSS form :
#li_4 { display:none !important; }
Make sure you've change those id.
MachForm Support
Posted 15 years ago # -
I've tried this but doesn't work.
No error though...
can you help?thanks a lot
Posted 12 years ago # -
Because of the time before the first message, I am trying to find the code in order to insert my own calculation field, but the code no longer exists. Was there ever a resolution with this or is there a way to do this now? I can work with the existing script using my own element fields, but need to know where to insert the code and whether it works. Maybe someone can help with MachForm3. BTW, Major Release 4.0 and no calculation fields?
Posted 10 years ago #
Reply
You must log in to post.