calculation within a form

E

elji

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.
 
L

Lee

elji said:
<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?

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.
 
E

elji

I would do it with onClick

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

elji

Lee said:
elji said:




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.


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.
 
T

Thomas 'PointedEars' Lahn

elji said:
<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

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)">
and when someone checks the second radiobutton (fast),
the "total" textfield shows: price x quantity + 10

<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.
Sadly, I don't know javascript,

But you can learn it.
but there's got to be an easy way to do that, right?

Yes, it is.


HTH

PointedEars
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top