Jump to content

Do Calculation with javascript in submission form


Recommended Posts

I have a submission form where the user clicks a link and it's supposed to do a simple calculation based on a few fields. I can't seem to get it to work right though. 

 

The fields are: 

MRP (currency)

Supplier_Discount (number)

Cost (currency)

 

It's a simple calculation I need to do. The user enters the MRP and Supplier Discount and clicks the link to calculate and fill in the Cost. 

 

The calculation is Cost=MRP-(MRP*Supplier_Discount) so if:

MRP = 1000

Supplier_Discount = .3

Cost = 700

 

I tested the link calling the js and it works (used a message to test it) and also tested simply doing Cost= MRP and that worked also. It's when I try to call Supplier_Discount and do a calculation that it's not working. 

 

I'm not sure if it's because Supplier_Discount is a number field and not currency or if I'm just missing some syntax but any help would be greatly appreciated. 

 

I have this in an html block at the bottom of the submission form: 

 

<SCRIPT LANGUAGE="JavaScript">

 

$("#k_cyourcost").click(function cyourcost(){

var MRP = document.getElementById("InsertRecordMRP").value;

var Supplier_Discount = document.getElementById("InsertRecordSupplier_Discount").value;

var Cost = (MRP)-((MRP)*(Supplier_Discount));

document.getElementById("InsertRecordCost").value= Cost;

}); 

 

</SCRIPT>

Link to comment
Share on other sites

I've also tried the below based on what I found in the Javascript section of the forum but to no avail:

<SCRIPT LANGUAGE="JavaScript">

$("#k_cyourcost").click(function calculate()
 {
   var v_MRP = parseFloat(document.getElementById("InsertRecordMRP").value);
   var v_Supplier_Discount  = parseFloat(document.getElementById("InsertRecordSupplier_Discount").value);
   var v_Cost  = (v_MRP * v_Supplier_Discount);
   document.getElementById("InsertRecordCost").value = Math.round(v_Cost);
 })

</SCRIPT>

Link to comment
Share on other sites

Hi MayMusic,

That works great- many thanks.  I'd just figured that out a few minutes ago for a related post I'd put up. It was the cascading text field part that had thrown me off. Thanks again for your ongoing help- these forums really make a difference. 

Link to comment
Share on other sites

Hi MayMusic,

I just realized the above formula isn't rounding for currency like I need it to. For example if:

MRP=5555 

Supplier_Discount=.5 

Cost=2778 when it should actually be 2777.50

How do I modify the below function to use currency rounding with 2 decimals? 

$("#k_cyourcost").click(function calculate()
 {
   var v_MRP = parseFloat(document.getElementById("EditRecordItems_MRP").value);
   var v_Supplier_Discount  = parseFloat(document.getElementsByName("EditRecordItems_Supplier_Discount")[0].value);
   var v_Cost  = (v_MRP-(v_MRP * v_Supplier_Discount));
   document.getElementById("EditRecordCost").value = Math.round(v_Cost);
 })
 

Link to comment
Share on other sites

  • 2 years later...

Hi @DesiLogi,

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 Cost field.

Check these videos for more information:

I hope this helps!

Link to comment
Share on other sites

  • 2 months later...

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...