Jump to content

Calculation on values entered in Submission Form Help


Recommended Posts

Hi,

I am trying to perform a calculation on one of the field "Amount" - the calculation is pretty much (regular hours x regular rate) + (overtime hours * overtime rate). I have the following javascript below but it does not seem to work. Please help me resolve this issue! 

Fields: Amount, Regular_Hours, OT_Hours, Rates and Rate_OT

 

<SCRIPT LANGUAGE="JavaScript">

 function calculate()
 {
   var v_hours = parseFloat(document.getElementById("InsertRecordRegular_Hours").value);

   var v_hoursot = parseFloat(document.getElementById("InsertRecordOT_Hours").value);
   
var v_rates  = parseFloat(document.getElementById("InsertRecordRates").value);

var v_ratesot  = parseFloat(document.getElementById("InsertRecordRate_OT").value);
   
var v_amount  = ((v_hours * v_rates)+(v_hoursot * v_ratesot));
   
document.getElementById("InsertRecordAmount").value = v_amount;
 }

 /* On submitting the webform, the function calculate is executed */
 document.getElementById("Regular_Hours").onchange=calculate;

 document.getElementById("OT_Hours").onchange=calculate;

</SCRIPT>

Link to post
Share on other sites

Hi rickin21,

You may try to change the triggering event to ".onsubmit".

<SCRIPT LANGUAGE="JavaScript">

 function calculate()
 {
   var v_hours = parseFloat(document.getElementById("InsertRecordRegular_Hours").value);

   var v_hoursot = parseFloat(document.getElementById("InsertRecordOT_Hours").value);
   
var v_rates  = parseFloat(document.getElementById("InsertRecordRates").value);

var v_ratesot  = parseFloat(document.getElementById("InsertRecordRate_OT").value);
   
var v_amount  = ((v_hours * v_rates)+(v_hoursot * v_ratesot));
   
document.getElementById("InsertRecordAmount").value = v_amount;
 }

 /* On submitting the webform, the function calculate is executed */
document.getElementById("caspioform").onsubmit=calculate;

</SCRIPT>

 

Hope this helps.

Link to post
Share on other sites
19 hours ago, rickin21 said:

Hi,

I am trying to perform a calculation on one of the field "Amount" - the calculation is pretty much (regular hours x regular rate) + (overtime hours * overtime rate). I have the following javascript below but it does not seem to work. Please help me resolve this issue! 

Fields: Amount, Regular_Hours, OT_Hours, Rates and Rate_OT

 

<SCRIPT LANGUAGE="JavaScript">

 function calculate()
 {
   var v_hours = parseFloat(document.getElementById("InsertRecordRegular_Hours").value);

   var v_hoursot = parseFloat(document.getElementById("InsertRecordOT_Hours").value);
   
var v_rates  = parseFloat(document.getElementById("InsertRecordRates").value);

var v_ratesot  = parseFloat(document.getElementById("InsertRecordRate_OT").value);
   
var v_amount  = ((v_hours * v_rates)+(v_hoursot * v_ratesot));
   
document.getElementById("InsertRecordAmount").value = v_amount;
 }

 /* On submitting the webform, the function calculate is executed */
 document.getElementById("Regular_Hours").onchange=calculate;

 document.getElementById("OT_Hours").onchange=calculate;

</SCRIPT>

Hi,

InsertRecord is missing before field names in the following lines:

 document.getElementById("Regular_Hours").onchange=calculate;

 document.getElementById("OT_Hours").onchange=calculate;

 

You may use onblur event in order to run function onchange, also I would recommend adding one more conditions in order to check that all the values which are being used in calculation are fulfilled with data

<SCRIPT LANGUAGE="JavaScript">

 function calculate()
 {
   var v_hours = parseFloat(document.getElementById("InsertRecordRegular_Hours").value);

   var v_hoursot = parseFloat(document.getElementById("InsertRecordOT_Hours").value);
   
var v_rates  = parseFloat(document.getElementById("InsertRecordRates").value);

var v_ratesot  = parseFloat(document.getElementById("InsertRecordRate_OT").value);

if (isNaN(v_hours) || v_hours.length<1) {
 v_hours = 0;
}else if (isNaN(v_hoursot) || v_hoursot.length<1) {
v_hoursot = 0;
}else if (isNaN(v_rates) || v_rates.length<1) {
v_rates = 0;
}else if (isNaN(v_ratesot ) || v_ratesot.length<1) {
v_ratesot = 0;
}

var v_amount  = ((v_hours * v_rates)+(v_hoursot * v_ratesot));
if ( isNaN(v_amount)){
v_amount = "";
}

document.getElementById("InsertRecordAmount").value = v_amount;
 }
 document.getElementById("InsertRecordRegular_Hours").onblur=calculate;
 document.getElementById("InsertRecordOT_Hours").onblur=calculate;

</SCRIPT>

Cheers!

Link to post
Share on other sites
  • 1 year later...

Hi @rickin21,

As of July 12, 2018, Caspio introduced new features which you can find here: https://howto.caspio.com/release-notes/caspio-bridge-13-0/

This release includes a new feature, Calculated Values in Submission Forms.  This allows you to generate calculations which you can use for your Amount.

Check these videos for more information:

I hope this helps!

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...