This forum is no longer open and is for reading/searching only.

Please use our new MachForm Community Forum instead.

MachForm Community Forums » MachForm 4

Create a field that calculates off other fields input


  1. garrettruff
    Member

    I have tried searching the forums for a while but never found a solution. I am looking for a way I can calculate square footage based on what my users insert in the length and width fields. Searching through the files I noticed there is a CSS file specific to the form ID but cannot find one with the functionality. I do want to get more complex later with summing the square footage totals then multiplying by a price per square foot field that will give me total cost but if someone can tell me how I can add the php code needed to make an element calculate and display the product I should be able to run with it. Not sure if this is something I could set up in the database to calculate and have the field post what the database has for that field. Or if anyone has done anything similar or knows where I could start or what files I would need to touch it would be greatly appreciated as I am a little lost on where to get started.

    (number field) Length = User enters data

    (number field) Width = User enters data

    (Calculate field or display) = The user data entered for length multiplied by the user entered data for width

    Posted 9 years ago #
  2. garrettruff
    Member

    I have looked further add it seems it would need to be a java script that is inserted on the includes/ view-functions.php
    I created a file with the script I think would work:

    $(document).ready(function () {

    var one, two, total;
    // JavaScript for form_10859
    if ($('#form_10859').length) {

    $('#element_28').prop('disabled', true);
    $(document).change(function () {
    one = $('#element_25').val();
    two = $('#element_27').val();
    total = parseFloat(one) * parseFloat(two);
    $('#element_28').val(parseFloat(total).toFixed(2));
    });
    }

    I have tried adding the path the script file in multiple places under <head> but still dosn't work. Anyone know if this is the right approach or what I may be missing?

    <head>;
    {$custom_script_js}
    <script type="text/javascript" src="{$machform_path}includes/Custom.js"></script>
    </head>

    Posted 9 years ago #
  3. williamansley
    Member

    @garrettruff: If you are running Machform 4.2, then you don't have to add the path to the script file to your code at all, you can use the new feature that lets you include the javascript file in your form properties. Edit the form, click on the "Form Properties" tab, click on the "show more options" link, scroll down to the bottom, and check the "Load Custom Javascript File" under "Advanced Option".

    Posted 9 years ago #
  4. web365
    Member

    Hello.

    May I know the detailed steps and the detailed javascript to include in the form properties to add the values of the 2 fields so that the third field will just show the sum of the 2 fields?

    Do I need to paste the actual javascript codes in the advanced option, or just the script URL? If it is the script URL, should I paste the code in a notepad and save it with .js? If I upload the .js file to /domain.com/machform/script/ should I enter in the URL /machform/script/xxx.js?

    Thank you.

    Posted 8 years ago #
  5. grayfletch
    Member

    I dont know if you have the answer for this so ill show example i used
    in the form properties - show more -load custom javascript file just put the url to your js file
    IE: http://localhost/machform/js/test.js

    as an example the test.js file saved in /machform/js/test.js

    $(document).ready(function () {

    var one, two, total;
    if ($('#form_18163').length) {
    $(document).change(function () {
    one = $('#element_4').val();
    two = $('#element_5').val();
    total = parseFloat(one) + parseFloat(two);
    $('#element_10').val(parseFloat(total).toFixed(2));

    });
    }
    });

    obviously the #form id and #element fields will be specific to your installation
    the #form id can be found when editing the form by the url in your browser
    IE: http://localhost/machform/edit_form.php?id=18163
    The #element fields can be found by going to
    notifications-Send Notification Emails to My Inbox-more options - press underlined template-variables
    -pull down list for variable ie suma it will show the #element id for this variable

    A test form could include 3 fields
    hide the total in this case element_10 with the logic setting until suma and sumb have some data

    Posted 8 years ago #

RSS feed for this topic

Reply