calculation within a form

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

    calculation within a form

    In my form, I have 4 objects that I want to work together:


    <input name="price" type="text" id="price" value="100" size="4">

    <input name="quantity" type="text" id="quantity" value="1" size="2">


    <input name="shipping" type="radio" value="slow">

    <input name="shipping" type="radio" value="fast">


    <input name="total" type="text" id="total" size="8">


    I 'd like that when someone checks the first radiobutton (slow),
    the "total" textfield shows: price x quantity + 5

    and when someone checks the second radiobutton (fast),
    the "total" textfield shows: price x quantity + 10


    Sadly, I don't know javascript, but there's got to be an easy way to do
    that, right?

    Thanks to all who will answer, any link to a page that has something
    similar would also be appreciated.

  • elji

    #2
    Re: calculation within a form

    I would do it with onClick

    <input name="shipping" type="radio" value="slow" onClick="...">



    elji wrote:
    [color=blue]
    > In my form, I have 4 objects that I want to work together:
    >
    >
    > <input name="price" type="text" id="price" value="100" size="4">
    >
    > <input name="quantity" type="text" id="quantity" value="1" size="2">
    >
    >
    > <input name="shipping" type="radio" value="slow">
    >
    > <input name="shipping" type="radio" value="fast">
    >
    >
    > <input name="total" type="text" id="total" size="8">
    >
    >
    > I 'd like that when someone checks the first radiobutton (slow),
    > the "total" textfield shows: price x quantity + 5
    >
    > and when someone checks the second radiobutton (fast),
    > the "total" textfield shows: price x quantity + 10
    >
    >
    > Sadly, I don't know javascript, but there's got to be an easy way to do
    > that, right?
    >
    > Thanks to all who will answer, any link to a page that has something
    > similar would also be appreciated.
    >[/color]

    Comment

    • Lee

      #3
      Re: calculation within a form

      elji said:
      [color=blue]
      ><input name="price" type="text" id="price" value="100" size="4">
      ><input name="quantity" type="text" id="quantity" value="1" size="2">
      ><input name="shipping" type="radio" value="slow">
      ><input name="shipping" type="radio" value="fast">
      ><input name="total" type="text" id="total" size="8">
      >
      >I 'd like that when someone checks the first radiobutton (slow),
      >the "total" textfield shows: price x quantity + 5
      >
      >and when someone checks the second radiobutton (fast),
      >the "total" textfield shows: price x quantity + 10
      >
      >
      >Sadly, I don't know javascript, but there's got to be an easy way to do
      >that, right?[/color]

      Is this for a class?
      It seems too simplistic to be for a real website, and I'd hate
      to think that somebody who doesn't know Javascript would try
      to create a commercial web site. You could open yourself to
      all sorts of legal and financial problems.

      Comment

      • elji

        #4
        Re: calculation within a form

        Lee wrote:
        [color=blue]
        > elji said:
        >
        >[color=green]
        >><input name="price" type="text" id="price" value="100" size="4">
        >><input name="quantity" type="text" id="quantity" value="1" size="2">
        >><input name="shipping" type="radio" value="slow">
        >><input name="shipping" type="radio" value="fast">
        >><input name="total" type="text" id="total" size="8">
        >>
        >>I 'd like that when someone checks the first radiobutton (slow),
        >>the "total" textfield shows: price x quantity + 5
        >>
        >>and when someone checks the second radiobutton (fast),
        >>the "total" textfield shows: price x quantity + 10
        >>
        >>
        >>Sadly, I don't know javascript, but there's got to be an easy way to do
        >>that, right?[/color]
        >
        >
        > Is this for a class?
        > It seems too simplistic to be for a real website, and I'd hate
        > to think that somebody who doesn't know Javascript would try
        > to create a commercial web site. You could open yourself to
        > all sorts of legal and financial problems.
        >[/color]


        No, that's not for a class, this looks simplistic, but that's only a
        part of a very large form, but I've only shown here where my problem is.

        And yes, I hardly know javascript, but my commerce is not about programming.


        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: calculation within a form

          Lee wrote:
          [color=blue]
          > Is this for a class?[/color]

          What do you mean by `class'?


          PointedEars

          Comment

          • Thomas 'PointedEars' Lahn

            #6
            Re: calculation within a form

            elji wrote:
            [color=blue]
            > <input name="price" type="text" id="price" value="100" size="4">
            > <input name="quantity" type="text" id="quantity" value="1" size="2">
            > <input name="shipping" type="radio" value="slow">
            > <input name="shipping" type="radio" value="fast">
            > <input name="total" type="text" id="total" size="8">
            >
            > I 'd like that when someone checks the first radiobutton (slow),
            > the "total" textfield shows: price x quantity + 5[/color]

            function calcTotal(o)
            {
            if (o
            && o.value
            && o.form
            && o.form.elements
            && o.form.elements['price']
            && o.form.elements['quantity']
            && o.form.elements['total'])
            {
            var total =
            o.form.elements['price'].value * o.form.elements['quantity'].value;
            var adds = {slow: 5, fast: 10};
            total += (adds[o.value] ? adds[o.value] : 0);
            o.form.elements['total'].value = total;
            }
            }
            ....
            <input name="shipping" type="radio" value="slow"
            onclick="calcTo tal(this)">
            [color=blue]
            > and when someone checks the second radiobutton (fast),
            > the "total" textfield shows: price x quantity + 10[/color]

            <input name="shipping" type="radio" value="fast"
            onclick="calcTo tal(this)">

            The calcTotal(...) function decides what to add
            depending on the `value' attribute of the radio
            button. Untested.
            [color=blue]
            > Sadly, I don't know javascript,[/color]

            But you can learn it.
            [color=blue]
            > but there's got to be an easy way to do that, right?[/color]

            Yes, it is.


            HTH

            PointedEars

            Comment

            • Fabian

              #7
              Re: calculation within a form

              Thomas 'PointedEars' Lahn hu kiteb:
              [color=blue]
              > Lee wrote:
              >[color=green]
              >> Is this for a class?[/color]
              >
              > What do you mean by `class'?[/color]

              Judging from context, I'd guess class as in a course of study.


              --
              --
              Fabian
              Visit my website often and for long periods!


              Comment

              Working...