Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > calculation within a form

Reply
Thread Tools

calculation within a form

 
 
elji
Guest
Posts: n/a
 
      11-05-2003
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.

 
Reply With Quote
 
 
 
 
Lee
Guest
Posts: n/a
 
      11-05-2003
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.

 
Reply With Quote
 
 
 
 
elji
Guest
Posts: n/a
 
      11-05-2003
I would do it with onClick

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



elji wrote:

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


 
Reply With Quote
 
elji
Guest
Posts: n/a
 
      11-05-2003
Lee wrote:

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



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.


 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      11-12-2003
Lee wrote:

> Is this for a class?


What do you mean by `class'?


PointedEars
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      11-12-2003
elji wrote:

> <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
 
Reply With Quote
 
Fabian
Guest
Posts: n/a
 
      11-12-2003
Thomas 'PointedEars' Lahn hu kiteb:

> Lee wrote:
>
>> Is this for a class?

>
> What do you mean by `class'?


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Form within a Form - will it cause problems when submitting? Dave Smithz HTML 3 10-30-2009 07:24 AM
How to retrieve form field value if form is EncType=multipart/form-dataForm? Li Zhang ASP .Net 4 02-27-2009 01:23 AM
calculation within datatable question Mike ASP .Net 6 03-10-2008 02:22 PM
Simple Calculation in Form - 3 textboxes - 1 function rdavis7408@gmail.com Javascript 9 01-25-2006 07:34 PM
Problem with date calculation in form Pete Javascript 6 12-31-2003 08:55 PM



Advertisments