Connecting Tech Pros Worldwide Forums | Help | Site Map

calculation within a form

elji
Guest
 
Posts: n/a
#1: Jul 20 '05
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
Guest
 
Posts: n/a
#2: Jul 20 '05

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]

Lee
Guest
 
Posts: n/a
#3: Jul 20 '05

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.

elji
Guest
 
Posts: n/a
#4: Jul 20 '05

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.


Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#5: Jul 20 '05

re: calculation within a form


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

What do you mean by `class'?


PointedEars
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#6: Jul 20 '05

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="calcTotal(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="calcTotal(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
Fabian
Guest
 
Posts: n/a
#7: Jul 20 '05

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!
http://www.lajzar.co.uk

Closed Thread