DesiLogi Posted June 17, 2016 Report Share Posted June 17, 2016 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> Quote Link to comment Share on other sites More sharing options...
DesiLogi Posted June 17, 2016 Author Report Share Posted June 17, 2016 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> Quote Link to comment Share on other sites More sharing options...
DesiLogi Posted June 17, 2016 Author Report Share Posted June 17, 2016 I should also note that the Supplier_Discount field is a cascading text field. Quote Link to comment Share on other sites More sharing options...
MayMusic Posted June 17, 2016 Report Share Posted June 17, 2016 Then you need to replace document.getElementById("InsertRecordSupplier_Discount").value with document.getElementsByName("InsertRecordSupplier_Discount")[0].value Quote Link to comment Share on other sites More sharing options...
DesiLogi Posted June 17, 2016 Author Report Share Posted June 17, 2016 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. Quote Link to comment Share on other sites More sharing options...
DesiLogi Posted June 20, 2016 Author Report Share Posted June 20, 2016 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); }) Quote Link to comment Share on other sites More sharing options...
DesiLogi Posted June 20, 2016 Author Report Share Posted June 20, 2016 Nevermind- I got it worked out via a support ticket. If anyone needs the solution it's replacing Math.round(v_Cost); with v_Cost.toFixed(2); Quote Link to comment Share on other sites More sharing options...
MayMusic Posted June 20, 2016 Report Share Posted June 20, 2016 Sounds good keep in mind http://www.w3schools.com/ is a very good source for JS and HTML questions you might have. Quote Link to comment Share on other sites More sharing options...
MayMusic Posted June 20, 2016 Report Share Posted June 20, 2016 Here is the one I could find on this topic: http://www.w3schools.com/jsref/jsref_tofixed.asp Quote Link to comment Share on other sites More sharing options...
Kurumi Posted October 22, 2018 Report Share Posted October 22, 2018 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! Quote Link to comment Share on other sites More sharing options...
DesiLogi Posted October 22, 2018 Author Report Share Posted October 22, 2018 Thanks guys- the updated tips are much appreciated! Quote Link to comment Share on other sites More sharing options...
JolliBeng Posted December 23, 2018 Report Share Posted December 23, 2018 Hi! Just to add more information about the Calculated Values: As of Dec. 6, 2016, Calculated Values can use other Calculated Values in Submission Forms. You may want to check Caspio Bridge 16.0 to know more details about the current release. -JolliBeng Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.