Jump to content
robelliott

Store Value Of Calculated Field

Recommended Posts

I need to do a simple calculation on a datapage and store the result in a field in the table. However the result is not being stored so I'm doing something stupid but can't immediately see what despite looking at the few examples here in the forum. So  I hope someone can help me out.

 

The table has a lot of fields, two of which are number (general) fields called Miles and usual_co2. The miles figure is entered into the dataform by the user. The usual_co2 is a cascading listbox based on the type of vehicle they are using. That figure is saved with no problem in the table.

 

I need to multiply the miles by the usual_co2 and store the result in the field calculated_co2 (also a number (general field) when the form is saved. The javascript calculation I have put into the footer of the datapage is:

<SCRIPT LANGUAGE="JavaScript">

 function calculate()
 {
   /* Retrieve the value of the field Miles and store in a variable v_miles */
   var v_miles = parseInt(document.getElementById("InsertRecordMiles").value);

   /* Retrieve the value of the field usual_co2 and store in a variable v_usual_co2 */
   var v_usual_co2 = parseInt(document.getElementById("InsertRecordusual_co2").value);

   /* Multiply the value Miles and usual_co2 to obtain v_calculated_co2 */
   var v_calculated_co2=(v_miles*v_usual_co2);

   document.getElementById("InsertRecordcalculated_co2").value = v_calculated_co2);
 }

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

</SCRIPT>

Can someone see what is wrong here? Thanks.

 

Rob

Share this post


Link to post
Share on other sites

Hi Rob,

 

To get the value of a dropdown element you need to use the following syntax:

 

document.getElementsByName("InsertRecordusual_co2")[0].value;

 

Try to replace them with the one above and see if that fixes the issue. If not, provide the page URL if possible so I can see any error using IE development tool (F12) or Firebug in FF. You can also use these tools for debugging your code.

 

Best,

Emma

Share this post


Link to post
Share on other sites

Calculated field in results page is generated on the fly each time results page is loaded. It cannot be saved into the table. Calculated field in web form can be saved into the table after submission by using JavaScript. In details page, you can create another field and make it hidden, then select "DataSource Fields" from "On load assign" dropdown and select your calculated field from the list. This enables passing the calculated field as parameter into the new field. Make sure passing parameter in your DataPage is enabled from the second wizard screen.

Share this post


Link to post
Share on other sites

I am not able to select the "Calculated Field" from the "Data Source Fields" drop down menu, it doesn't appear there. 

 

I am in the same situation. I have a "Calculated Field" that runs a calculation which I need stored in a table. I am unable to get this calculated value to get stored in the table currently. 

 

Any assistance would be greatly appreciated!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×