Results 1 to 4 of 4

Thread: calculations during form data input stage

  1. #1
    Join Date
    Feb 2008
    Posts
    27

    Default calculations during form data input stage

    When a user enters a value in one of the fields in a form I want to carry out a calculation and display the results of this calculation before proceeding on to the next field, and all before final submit is pressed.
    Part of this process also to take a couple of values previously entered, sum them and show the result before proceeding further with the form.
    This will allow the applicant to see if the value he has entered is sensible and will help in interpreting results of data entered later on.
    I would prefer to do this in php but not sure how to (as it were) interrupt the input process. I am sure it must be possible as I have seen forms where instant messages are displayed as part of the input process.


  2. #2
    Join Date
    Dec 2003
    Posts
    3,980

    Default Re: calculations during form data input stage

    Hi,

    To do this, you need to write JavaScript code.

    There are two places these calculations can be performed:
    • in the browser
    • on the server
    It is much easier to do the calculations in the browser. This means some JavaScript code in the browser (i.e. in the HTML page) that performs the calculations you want.

    To do them on the server, you need to create an Ajax solution. This means JavaScript code in the browser (i.e. in the HTML page) and PHP code on the server.

    Either method involves programming, and I strongly recommend you hire a programmer to do this. The server method is very complicated and needs a lot of experience to get right.

    To get you started with the browser solution:
    1. Load jQuery into your HTML page. Put this in the <head> section of your page:
      Code:
      <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    2. To display when a field (say it's called amount - with name="amount"), add this to your page just before the </body> close tag:
      Code:
      <script type="text/javascript">$('input[name=amount]').change(function () { alert($(this).val()); });</script>
    If you can understand what that's doing, then you can progress to adding field values together.

    I hope that helps.
    Russell Robinson - Author of Tectite FormMail and FormMailDecoder
    http://www.tectite.com/

  3. #3
    Join Date
    Feb 2008
    Posts
    27

    Default Re: calculations during form data input stage

    Thank you for that reply which is useful and I expect the browser (html code) option if achievable would be best.
    Let me be more specific re one of my requirements.
    Okay, I have within a form the following:
    <input name="txgain" type="text" size="6" maxlength="10" />
    and elsewhere:
    <input name="losses" type="text" size="8" maxlength="10" />
    and a third entry:
    <input name="powerout" type="text" size="8" maxlength="10" />
    What I actually want to do is bring up a pop-up message when the applicant puts in a value in the third box, namely:

    poweroutdbw=10*log10(powerout);
    ERPcalc=poweroutdbw-losses+txgain;


    The pop-up message will display ERPcalc rounded to 1 decimal place, and the applicant can either carry on if this is okay, or go back and amend one or more of the three offending fields.
    All that to happen BEFORE submit is pressed.

  4. #4
    Join Date
    Dec 2003
    Posts
    3,980

    Default Re: calculations during form data input stage

    Hi,

    I'm happy to give suggestions, but I can't really provide JavaScript programming instructions in a FormMail Support Forum.

    This should be close:
    HTML Code:
    <script type="text/javascript">$('input[name=txgain],input[name=losses],input[name=powerout]').change(function () {
       var dbw = Math.log($('input[name=powerout]').val()) / Math.log(10);
      alert(dbw-input[name=losses]').val()+input[name=txgain]').val());
     
    });</script>
    Russell Robinson - Author of Tectite FormMail and FormMailDecoder
    http://www.tectite.com/

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Reloading Form Data after Verification Error - Data Lost
    By MB1711 in forum Community Support
    Replies: 2
    Last Post: 02-Dec-2010, 03:16 AM
  2. Replies: 1
    Last Post: 14-Sep-2010, 06:54 PM
  3. field validation in 2 stage form
    By charlieboy in forum Community Support
    Replies: 1
    Last Post: 25-Jan-2010, 08:36 PM
  4. calculations with formail
    By erikstoop in forum Community Support
    Replies: 1
    Last Post: 21-Sep-2009, 05:35 PM
  5. two stage form handling?
    By piemanek in forum Community Support
    Replies: 0
    Last Post: 10-Jul-2006, 04:02 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •