Gossamer Forum
Home : Products : DBMan : Customization :

On-the-Fly field calculation mod

Quote Reply
On-the-Fly field calculation mod
Hi,
I am using the DBMAN program to store records for a business. In my orders database, I have a a couple of fields that I would like the system to calculate for me and then write to the database file. For example:
I have a field named SUBTOTAL and SALESTAX and LINETOTAL. I would like to be able to enter information into the SUBTOTAL and SALESTAX field, and then have the LINETOTAL calculate the function SUBTOTAL + (SUBTOTAL*SALESTAX). However, I want the script to do this on-the-fly, so that when I click in the LINETOTAL field, the answer to the calculation appears, and then when I click on the add record button all of the fields are stored in the database. Please help me out with the code for this. Thanks in advance.

------------------
-- I call it a "Display of Affection," But the police call it "Indecent Exposure." Go Figure.
Quote Reply
Re: On-the-Fly field calculation mod In reply to
You may want to look at this recent thread for ideas on how to do this:

http://www.gossamer-threads.com/scripts/forum/resources/Forum12/HTML/002739.html

Quote Reply
Re: On-the-Fly field calculation mod In reply to
I think Lois meant http://www.gossamer-threads.com/...m12/HTML/002724.html .

The other thread talks about doing the computation when a record is added. In order to do the computation while still looking at the form (which is what I think you wanted), you would have to use javascript.

Possibly someone else can help you with javascript. That's not my strong point.


------------------
JPD






Quote Reply
Re: On-the-Fly field calculation mod In reply to
In html_record_form, add javascript just before starting the record table (before the "print qq|" line):

Code:
print qq~
<script language="JavaScript">
function calculate(form1)
{
var subt = parseFloat(form1.subtotal.value);
var sale = parseFloat(form1.salestax.value);
var tax = subt*sale;
var tprice = subt+tax;
form1.linetotal.value = tprice
}
</script>
~;

At the lines where you define subtotal and salestax within your record form, add "OnChange="calculate(this.form)", as follows:

Code:
<INPUT TYPE="TEXT" NAME="subtotal" SIZE="6" MAXLENGTH="10" VALUE="$rec{'subtotal'}" OnChange="calculate(this.form)">

This should calculate the price on the fly....if you don't have a default entry for salestax, you may only want it to calculate after the salestax has been entered rather than both upon entry of subtotal and salestax.

Also, of course, change "subtotal", "salestax", and "linetotal" in the javascript to whatever these fields are called in your database.

This is also assuming sales tax is entered in the form .075. If you wish it to be entered as 7.5, use the following script instead:

Code:
print qq~
<script language="JavaScript">
function calculate(form1)
{
var subt = parseFloat(form1.subtotal.value);
var sale = form1.salestax.value;
var stax = parseFloat(sale/100);
var tax = subt*stax;
var tprice = subt+tax;
form1.linetotal.value = tprice
}
</script>
~;

I have something similar working on one of my databases, and it seems to work well. When you add the record, the values in each of those fields will be saved.

Melanie


[This message has been edited by msmoore (edited April 24, 2000).]

[This message has been edited by msmoore (edited April 24, 2000).]