Simple price x quantity

M

Mark 123

Hi

I have a form with the input fields:

ProductName
Quantity

and output fields:

ProductPrice
TotalUSD

Here's what I want it to do:

a) After either the ProductName or Quantity fields are updated by the user,
I want the resulting calculation to be displayed for the Product Price and
the TotalUSD. The ProductName is a drop-down with the product name as the
description and the value the dollar amount of each product.

b) I want the output fields to be displayed as $ format.

c) I want it so that the output fields can't be manually overwritten by the
user and later the form will be posted to a database.



I began with the following but are not familiar with the syntax of
JavaScript and got syntax errors. I also need the syntax for the AfterUpdate
event for the two input fields. Can you help?

I think I'll name the form "Form1".

<SCRIPT LANGUAGE="JavaScript">
Function CalculateTotal(form) {
form.ProductPrice.value=(form.ProductName.value);
form.TotalUSD.value= (form.ProductName.value * form.Quantity.value)
}
</SCRIPT>
 
R

Randy Webb

Mark said:
Hi

I have a form with the input fields:

ProductName
Quantity

and output fields:

ProductPrice
TotalUSD

Here's what I want it to do:

a) After either the ProductName or Quantity fields are updated by the user,
I want the resulting calculation to be displayed for the Product Price and
the TotalUSD. The ProductName is a drop-down with the product name as the
description and the value the dollar amount of each product.

b) I want the output fields to be displayed as $ format.

c) I want it so that the output fields can't be manually overwritten by the
user and later the form will be posted to a database.



I began with the following but are not familiar with the syntax of
JavaScript and got syntax errors. I also need the syntax for the AfterUpdate
event for the two input fields. Can you help?

There is no "AfterUpdate" event. You want the onChange event.

onchange="CalculateTotal(this.form)"
I think I'll name the form "Form1".

<SCRIPT LANGUAGE="JavaScript">

Function CalculateTotal(form) {

There is your syntax error, its function, not Function. Case Matters.
form.ProductPrice.value=(form.ProductName.value);
form.TotalUSD.value= (form.ProductName.value * form.Quantity.value)

Append the $ before setting the value, or better, as you set it.

"$" + (form.ProductName.value * form.Quantity.value)
 
M

Mark 123

Thanks. Regarding the currency, what if the multiplication causes greater
(or less) than 2 decimal places?
 
R

Randy Webb

Mark said:

<SCRIPT LANGUAGE="JavaScript"
<script type="text/javascript">

function CalculateTotal(form)

{
form.ProductPrice.value=(form.ProductName.value);
form.TotalUSD.value= (form.ProductName.value * form.Quantity.value)
}

</SCRIPT>

That is a copy/paste. See the double script tags? Its throwing a syntax
error and then stops. Get rid of the extra script tag, and then validate
your HTML:
www.w3c.org

http://diveintomark.org/archives/2003/05/05/why_we_wont_help_you

While looking through that code, I noticed you are using onblur to
validate your fields. That unnecessary. Use the onChange, or the
onSubmit event handlers.

<input type="text" size="8" name="Quantity" class="textbx" tabindex="5"
onblur="check1(this.form1)" onchange="CalculateTotal(this.form)"
value="1">

onChange="check1(this.form);CalculateTotal(this.form)"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top