Jump to content

Help with Datatables/Javascript


Recommended Posts

Hi all,

I'm struggling a bit and seem to have exhausted my ability to find answers based on previous questions. I'm trying to create an interactive database that receives information, adds it to a preexisting datasource, performs a basic calculation, and then compares it to a second existing data source.

The preexisting datasource is in a Table with names and associated usage values. When a user enters their name their record will be pulled up. In order to provide a standardized result, I need the user to enter the number of residents in their household so that I can then divide by the original number to create a standardized score. The number of residents that they enter is added to the preexisting datasource.

I'm getting bogged down in the second part - calculating and comparing to a second existing data source.

I used a javascript embed into an HTML code (appended below) to do the calculations. I couldn't get the calculation to run, perhaps because one of the values (the numerator) already existed in the database and the other value (the denominator) was being entered by the user in the form. Do I need to identify the values differently?

As an end-run around that, I created a second datapage that would accompany the results page on the website. This datapage drew on a table that listed values that correspond to all potential scores created in the first step (the product of the first value divided by the second value) and a corresponding population percentile that I would want to report to the user based on their standardized usage. On this datapage, I created virtual ID's for the two values to force the user to reenter both values and then inserted the javascript to perform the calculation there. That didn't seem to work nor did there seem to be an obvious way to take that result and find the corresponding value/percentile in the data table. It seems as though I really need to do the calculation in the first part of the process.

Ultimately, here's the sequence of events that I would like to occur:

1) User finds their record in a search box

2) User inputs number in household

3) Standardized score is calculated and entered into data table

4) Standardized score is used to find corresponding percentile in second data table and reported to user.

Thoughts?

Javascript used:

function calculate()

{

var v_Use_gallons_mth = parseFloat(document.getElementById("Use_gallons_mth").value);

var v_residents = parseFloat(document.getElementById("residents").value);

var v_use_each_res_mnthly = (v_Use_gallons_mth / v_residents);

document.getElementById("use_each_res_mnthly").value = Math.round(v_use_each_res_mnthly);

}

/* On submitting the webform, the function calculate is executed */

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

Link to comment
Share on other sites

hi waterscore,

Are the elements : 'Use_gallons_mth', 'residents' and 'use_each_res_mnthly' that you have used in the script, the real field name in your table?

Are these script written in a submission form or update form?

I asked these questions because caspio has different way for detecting an element.

If it is a submission form, then you have to write document.getElementById("InsertRecordUse_gallons_mth") instead of document.getElementById("Use_gallons_mth").

And if it is an update form then you have to use

document.getElementById("EditRecordUse_gallons_mth") instead of document.getElementById("Use_gallons_mth").

have a look at this topic in forum: viewtopic.php?f=14&t=12233&p=14077&hilit=Insertrecord#p14077

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

×
×
  • Create New...