Simple price x quantity

Discussion in 'Javascript' started by Mark 123, Aug 31, 2004.

  1. Mark 123

    Mark 123 Guest

    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>
    Mark 123, Aug 31, 2004
    #1
    1. Advertising

  2. Mark 123

    Randy Webb Guest

    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
    Randy Webb, Aug 31, 2004
    #2
    1. Advertising

  3. Mark 123

    Mark 123 Guest

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




    "Randy Webb" <> wrote in message
    news:...
    > 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
    Mark 123, Aug 31, 2004
    #3
  4. Mark 123

    Mark 123 Guest

    I can't get that event to fire:

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


    "Randy Webb" <> wrote in message
    news:...
    > 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
    Mark 123, Aug 31, 2004
    #4
  5. Mark 123

    Randy Webb Guest

    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/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)"

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq
    Randy Webb, Aug 31, 2004
    #5
  6. Mark 123

    Randy Webb Guest

    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
    Randy Webb, Aug 31, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nav

    time quantity in vhdl

    Nav, Oct 1, 2003, in forum: VHDL
    Replies:
    3
    Views:
    16,217
  2. Gary Vidal
    Replies:
    2
    Views:
    1,866
    Steven Cheng[MSFT]
    Feb 12, 2004
  3. Przemek Klein
    Replies:
    0
    Views:
    362
    Przemek Klein
    Oct 6, 2005
  4. Nandhu
    Replies:
    0
    Views:
    424
    Nandhu
    Dec 4, 2007
  5. Martina
    Replies:
    2
    Views:
    131
    Anno Siegel
    Sep 10, 2005
Loading...

Share This Page