By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,742 Members | 793 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,742 IT Pros & Developers. It's quick & easy.

Simple price x quantity

P: n/a
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>
Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Mark 123 wrote:
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">
<script type="text"/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)
}
</SCRIPT>


--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #2

P: n/a
Thanks. Regarding the currency, what if the multiplication causes greater
(or less) than 2 decimal places?


"Randy Webb" <Hi************@aol.com> wrote in message
news:cp********************@comcast.com...
Mark 123 wrote:
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">


<script type="text"/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)
}
</SCRIPT>


--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #3

P: n/a
I can't get that event to fire:

http://www.orbisoft.com/promo/doubleoffer_new.aspx
"Randy Webb" <Hi************@aol.com> wrote in message
news:cp********************@comcast.com...
Mark 123 wrote:
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">


<script type="text"/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)
}
</SCRIPT>


--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #4

P: n/a
Mark 123 wrote:
I can't get that event to fire:

http://www.orbisoft.com/promo/doubleoffer_new.aspx

<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/200..._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.fo rm)"

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #5

P: n/a
Mark 123 wrote:
Thanks. Regarding the currency, what if the multiplication causes greater
(or less) than 2 decimal places?


http://jibbering.com/faq/#FAQ4_6

And please don't top-post (That is also covered in the FAQ)

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.