Simple price x quantity

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark 123

    Simple price x quantity

    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="JavaS cript">
    Function CalculateTotal( form) {
    form.ProductPri ce.value=(form. ProductName.val ue);
    form.TotalUSD.v alue= (form.ProductNa me.value * form.Quantity.v alue)
    }
    </SCRIPT>


  • Randy Webb

    #2
    Re: Simple price x quantity

    Mark 123 wrote:[color=blue]
    > 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?[/color]

    There is no "AfterUpdat e" event. You want the onChange event.

    onchange="Calcu lateTotal(this. form)"
    [color=blue]
    > I think I'll name the form "Form1".
    >
    > <SCRIPT LANGUAGE="JavaS cript">[/color]

    <script type="text"/javascript">
    [color=blue]
    > Function CalculateTotal( form) {[/color]

    There is your syntax error, its function, not Function. Case Matters.
    [color=blue]
    > form.ProductPri ce.value=(form. ProductName.val ue);
    > form.TotalUSD.v alue= (form.ProductNa me.value * form.Quantity.v alue)[/color]

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

    "$" + (form.ProductNa me.value * form.Quantity.v alue)
    [color=blue]
    > }
    > </SCRIPT>[/color]



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

    Comment

    • Mark 123

      #3
      Re: Simple price x quantity

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




      "Randy Webb" <HikksNotAtHome @aol.com> wrote in message
      news:cpKdnTU1J8 efdK7cRVn-iw@comcast.com. ..[color=blue]
      > Mark 123 wrote:[color=green]
      > > 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[/color][/color]
      user,[color=blue][color=green]
      > > I want the resulting calculation to be displayed for the Product Price[/color][/color]
      and[color=blue][color=green]
      > > the TotalUSD. The ProductName is a drop-down with the product name as[/color][/color]
      the[color=blue][color=green]
      > > 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[/color][/color]
      the[color=blue][color=green]
      > > 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[/color][/color]
      AfterUpdate[color=blue][color=green]
      > > event for the two input fields. Can you help?[/color]
      >
      > There is no "AfterUpdat e" event. You want the onChange event.
      >
      > onchange="Calcu lateTotal(this. form)"
      >[color=green]
      > > I think I'll name the form "Form1".
      > >
      > > <SCRIPT LANGUAGE="JavaS cript">[/color]
      >
      > <script type="text"/javascript">
      >[color=green]
      > > Function CalculateTotal( form) {[/color]
      >
      > There is your syntax error, its function, not Function. Case Matters.
      >[color=green]
      > > form.ProductPri ce.value=(form. ProductName.val ue);
      > > form.TotalUSD.v alue= (form.ProductNa me.value * form.Quantity.v alue)[/color]
      >
      > Append the $ before setting the value, or better, as you set it.
      >
      > "$" + (form.ProductNa me.value * form.Quantity.v alue)
      >[color=green]
      > > }
      > > </SCRIPT>[/color]
      >
      >
      >
      > --
      > Randy
      > comp.lang.javas cript FAQ - http://jibbering.com/faq[/color]


      Comment

      • Mark 123

        #4
        Re: Simple price x quantity

        I can't get that event to fire:




        "Randy Webb" <HikksNotAtHome @aol.com> wrote in message
        news:cpKdnTU1J8 efdK7cRVn-iw@comcast.com. ..[color=blue]
        > Mark 123 wrote:[color=green]
        > > 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[/color][/color]
        user,[color=blue][color=green]
        > > I want the resulting calculation to be displayed for the Product Price[/color][/color]
        and[color=blue][color=green]
        > > the TotalUSD. The ProductName is a drop-down with the product name as[/color][/color]
        the[color=blue][color=green]
        > > 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[/color][/color]
        the[color=blue][color=green]
        > > 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[/color][/color]
        AfterUpdate[color=blue][color=green]
        > > event for the two input fields. Can you help?[/color]
        >
        > There is no "AfterUpdat e" event. You want the onChange event.
        >
        > onchange="Calcu lateTotal(this. form)"
        >[color=green]
        > > I think I'll name the form "Form1".
        > >
        > > <SCRIPT LANGUAGE="JavaS cript">[/color]
        >
        > <script type="text"/javascript">
        >[color=green]
        > > Function CalculateTotal( form) {[/color]
        >
        > There is your syntax error, its function, not Function. Case Matters.
        >[color=green]
        > > form.ProductPri ce.value=(form. ProductName.val ue);
        > > form.TotalUSD.v alue= (form.ProductNa me.value * form.Quantity.v alue)[/color]
        >
        > Append the $ before setting the value, or better, as you set it.
        >
        > "$" + (form.ProductNa me.value * form.Quantity.v alue)
        >[color=green]
        > > }
        > > </SCRIPT>[/color]
        >
        >
        >
        > --
        > Randy
        > comp.lang.javas cript FAQ - http://jibbering.com/faq[/color]


        Comment

        • Randy Webb

          #5
          Re: Simple price x quantity

          Mark 123 wrote:[color=blue]
          > I can't get that event to fire:
          >
          > http://www.orbisoft.com/promo/doubleoffer_new.aspx
          >[/color]

          <SCRIPT LANGUAGE="JavaS cript"[color=blue]
          ><script type="text/javascript">[/color]

          function CalculateTotal( form)

          {
          form.ProductPri ce.value=(form. ProductName.val ue);
          form.TotalUSD.v alue= (form.ProductNa me.value * form.Quantity.v alue)
          }

          </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:




          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="Calcu lateTotal(this. form)"
          value="1">

          onChange="check 1(this.form);Ca lculateTotal(th is.form)"

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

          Comment

          • Randy Webb

            #6
            Re: Simple price x quantity

            Mark 123 wrote:
            [color=blue]
            > Thanks. Regarding the currency, what if the multiplication causes greater
            > (or less) than 2 decimal places?[/color]



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

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

            Comment

            Working...