Jump to content

Need help with Java math calculation


Recommended Posts

I am trying to maintain a agent's balance once they sell an item. I have two tables, "agents" and "orders" as well as corresponding datapages. It currently works like this:

1. Agent account is created using "agent" datapage with initial "balance" field being entered.

2. Agent logs into "orders" datapage after being authenticated by user/pass from agent table.

3. Agent balance from "agent" table is displayed on "orders" form.

4. Agent selects item which has a value of, let's say $ 10.

5. Once the agent selects "submit" I would like to have the $ amount of sale deducted from the balance field (agent table) and the result displayed on the "order" page that the agent is currently logged into. The balance field is linked to the agent table but no changes occur after order is submitted.

I'm new to Caspio and don't know squat about Java. We were assured during our trial that this could be done. Here's what I have:

function calculate()

{

//(1) Retrieve the value of the field Balance and store in a variable 'balance'.

var balance = parseFloat(document.forms[0].InsertRecordBalance.value);

//(2) Retrieve the value of the field Topup_Amt and store in a variable 'topup_amt'.

var topup_amt = parseFloat(document.forms[0].InsertRecordTopup_Amt.value);

//(3) Subtract Topup_Amt from the value Balance to obtain the value of 'balance'.

var balance = (balance - topup_amt);

//(4) Insert a rounded value of the variable 'balance' into the DataPage field Area.

document.forms[0].InsertRecordBalance.value = Math.round(area);

}

//(5) On submitting the webform, the function calculate is executed.

document.forms[0].onsubmit=calculate;

I don't know if I'm even close here but I don't understand how the above code knows what forms it's dealing with. Each line references form[0] and I'm using two forms.

Any help would be greatly appreciated. Thank you.

Link to comment
Share on other sites

Hello,

Since you mentioned using two forms, I'd suggest using 'getElementById' instead of forms[0] to access the field(s) on the form.

Here's how I'd do this:

function calculate()

{

//(1) Retrieve the value of the field Balance and store in a variable 'balance'.

var field1 = document.getElementById("InsertRecordBalance");

var balance = parseFloat(field1.value,10);

//(2) Retrieve the value of the field Topup_Amt and store in a variable 'topup_amt'.

var field2 = document.getElementById("InsertRecordTopup_Amt");

var topup_amt= parseFloat(field2.value,10);

//(3) Subtract Topup_Amt from the value Balance to obtain the value of 'balance'.

var tmp = (balance - topup_amt);

//(4) Insert a rounded value of the variable 'balance' into the DataPage field Area.

document.getElementById("InsertRecordBalance").value = tmp.toFixed(2);

}

//(5) On submitting the webform, the function calculate is executed.

document.getElementById("caspioform").onsubmit= calculate;

Hope this helps. Thanks!

SO

Link to comment
Share on other sites

  • 9 years later...

Hi @warwick,

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

Check these videos for more information:

 

 

 

Link to comment
Share on other sites

  • 2 years later...

Hi Just to add to the previous comment above, you may also find these video tutorials useful:

 

And just an update, you can now add the Calculated Value form element to Update Forms and Details Pages to generate dynamic calculations as users interact with your forms. Previously, this feature was only available in Submission Forms. You may check this link: https://howto.caspio.com/release-notes/caspio-bridge-17-0/

Link to comment
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...