473,406 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Calculated fields


I know absolutely nothing about JavaScript but I am told that JavaScrip
is needed to solve my Form problem. I’m trying to figure out how t
take 2 List fields that would have text names but would represen
numeric values and calculate them..

First List field would be like this:

Example 1:

<option>Sponsor One</option> (This would represent a numeric valu
of $40.00)
<option>Sponsor Two</option> (This would represent a numeric valu
of $25.00)
<option>Sponsor Three</option> (This would represent a numeric valu
of $10.00)

Then there would be a second List field as in Example 2

Example 2:

<option>1 Month</option> (This would represent a numeric value o
1)
<option>2 Months</option> (This would represent a numeric value o
2)
<option>3 Months</option> (This would represent a numeric value o
3)

Then I would like to multiply the numeric value of the selected item i
the first List field by the numeric value of the selected item in th
second List field and put the result into a text field called “Price”.

I would like to have the result appear in the Price fiel
automatically, as soon as the two items have been selected, withou
having to click on a button.

How difficult is this?

Thanks
Joh

John

Jul 20 '05 #1
10 1673
<snip problem description>
How difficult is this?


Very easy.

See W3C for the Select form control:
http://www.w3.org/TR/html401/interact/forms.html#h-17.6
(And don't just click on the link.... remember the site www.w3.org!)

That should solve the "display text" and "contains numerical values"
problem.

Also mentioned in above link is that Select form control has the onchange
event:
http://www.w3.org/TR/html401/interac...#adef-onchange

That should be the place to start the calculations.
Jul 20 '05 #2
On Tue, 17 Feb 2004 00:38:15 -0600, John C
<Jo***********@mail.forum4designers.com> wrote:
I know absolutely nothing about JavaScript but I am told that JavaScript
is needed to solve my Form problem. I�m trying to figure out how to
take 2 List fields that would have text names but would represent
numeric values and calculate them..
[snip]
Then I would like to multiply the numeric value of the selected item in
the first List field by the numeric value of the selected item in the
second List field and put the result into a text field called �Price�.

I would like to have the result appear in the Price field
automatically, as soon as the two items have been selected, without
having to click on a button.


Though KC Wong gave you a good start, you said you know nothing about
JavaScript, so I think a full solution would be more appropriate. Below is
a sample that should set you on your way.

<script type="text/javascript">
function updatePrice( formRef ) {
var rate = parseFloat( formRef.rate.value );
var time = parseInt( formRef.time.value, 10 );

if( isNaN( rate ) || isNaN( time )) {
formRef.price.value = formRef.price.defaultValue;
} else {
formRef.price.value = '$' + ( rate * time );
}
}
</script>
...
<form ... name="sponsor">
<select name="rate" onchange="updatePrice(this.form)">
<option value="n/a">Choose sponsorship option</option>
<option value="40.0">Sponsor One</option>
<option value="25.0">Sponsor Two</option>
<option value="10.0">Sponsor Three</option>
</select>
<select name="time" onchange="updatePrice(this.form)">
<option value="n/a">Choose sponsorship period</option>
<option value="1">1 Month</option>
<option value="2">2 Months</option>
<option value="3">3 Months</option>
</select>
<input name="price" type="text" value="Please choose values">
</form>

Hope that helps,
Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #3

KC Wong wrote:
*<snip problem description>
How difficult is this?


Very easy.

See W3C for the Select form control:
http://www.w3.org/TR/html401/interact/forms.html#h-17.6
(And don't just click on the link.... remember the site www.w3.org!)

That should solve the "display text" and "contains numerical values"
problem.

Also mentioned in above link is that Select form control has th
onchange
event:
http://tinyurl.com/3aaky

That should be the place to start the calculations.


John

Jul 20 '05 #4

Thank you very much. I'll check out the site. It may be a good plac
to start learning JavaScript.

Thanks
Joh

John

Jul 20 '05 #5

Michael Winter wrote:
*On Tue, 17 Feb 2004 00:38:15 -0600, John C
<Jo***********@mail.forum4designers.com> wrote:
I know absolutely nothing about JavaScript but I am told tha

JavaScript
is needed to solve my Form problem. I�m trying to figure out ho

to
take 2 List fields that would have text names but would represent
numeric values and calculate them..


[snip]
Then I would like to multiply the numeric value of the selecte

item in
the first List field by the numeric value of the selected item i

the
second List field and put the result into a text field calle

�Price�.

I would like to have the result appear in the Price field
automatically, as soon as the two items have been selected

without
having to click on a button.


Though KC Wong gave you a good start, you said you know nothin
about
JavaScript, so I think a full solution would be more appropriate
Below is
a sample that should set you on your way.

<script type="text/javascript">
function updatePrice( formRef ) {
var rate = parseFloat( formRef.rate.value );
var time = parseInt( formRef.time.value, 10 );

if( isNaN( rate ) || isNaN( time )) {
formRef.price.value = formRef.price.defaultValue;
} else {
formRef.price.value = '$' + ( rate * time );
}
}
</script>
...
<form ... name="sponsor">
<select name="rate" onchange="updatePrice(this.form)">
<option value="n/a">Choose sponsorship option</option>
<option value="40.0">Sponsor One</option>
<option value="25.0">Sponsor Two</option>
<option value="10.0">Sponsor Three</option>
</select>
<select name="time" onchange="updatePrice(this.form)">
<option value="n/a">Choose sponsorship period</option>
<option value="1">1 Month</option>
<option value="2">2 Months</option>
<option value="3">3 Months</option>
</select>
<input name="price" type="text" value="Please choose values">
</form>

Hope that helps,
Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" t
reply)


John

Jul 20 '05 #6

Thank you very much for your help. I will try your code and hopefull
will learn from it as well. I really appreciate your help.

Would you be able to recommend either a good book or an online tutoria
that would get me started in learning JavaScript? I see that there ar
a number of books in stores but books are like anything else,... ther
are some really good ones and there are some that are a waste of tim
and money. :)

Thanks again, much appreciated,

Joh

John

Jul 20 '05 #7

Michael Winter wrote:
*On Tue, 17 Feb 2004 00:38:15 -0600, John C
<Jo***********@mail.forum4designers.com> wrote:
I know absolutely nothing about JavaScript but I am told tha

JavaScript
is needed to solve my Form problem. I�m trying to figure out ho

to
take 2 List fields that would have text names but would represent
numeric values and calculate them..


[snip]
Then I would like to multiply the numeric value of the selecte

item in
the first List field by the numeric value of the selected item i

the
second List field and put the result into a text field calle

�Price�.

I would like to have the result appear in the Price field
automatically, as soon as the two items have been selected

without
having to click on a button.


Though KC Wong gave you a good start, you said you know nothin
about
JavaScript, so I think a full solution would be more appropriate
Below is
a sample that should set you on your way.

<script type="text/javascript">
function updatePrice( formRef ) {
var rate = parseFloat( formRef.rate.value );
var time = parseInt( formRef.time.value, 10 );

if( isNaN( rate ) || isNaN( time )) {
formRef.price.value = formRef.price.defaultValue;
} else {
formRef.price.value = '$' + ( rate * time );
}
}
</script>
...
<form ... name="sponsor">
<select name="rate" onchange="updatePrice(this.form)">
<option value="n/a">Choose sponsorship option</option>
<option value="40.0">Sponsor One</option>
<option value="25.0">Sponsor Two</option>
<option value="10.0">Sponsor Three</option>
</select>
<select name="time" onchange="updatePrice(this.form)">
<option value="n/a">Choose sponsorship period</option>
<option value="1">1 Month</option>
<option value="2">2 Months</option>
<option value="3">3 Months</option>
</select>
<input name="price" type="text" value="Please choose values">
</form>

Hope that helps,
Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" t
reply)


John

Jul 20 '05 #8

Hi Mike,

I don't want to be a pest but,...

I tried your code but ran into an error. I copied the code as you ha
given it and it seems to run fine but as soon as you select an optio
from either <select> menu it flags an error that says:

A Runtime error has occurred.

Line 7:
Error: 'time.value' is null or not an object.

Any suggestions? The code I entered is as follows;
<html>

<head>
<title>Calculated Fields</title>
<script type="text/Javascript">
function updatePrice( formRef ){
var rate = parseFloat( formRef.rate.value );
var time = parseInt( formRef.time.value,10 );

if(isNaN( rate ) || isNaN( time )){
formRef.price.value = formRef.price.defaultValue;
}else{
formRef.price.value = '$' + ( rate * time );
}
}
</script>
</head>

<body>

<form name="sponsor" method="post">
<select name="rate" onchange="updatePrice(this.form)">
<Option value="n/a">Choose Sponsorship</option>
<Option value="40.0">Site Sponsor</option>
<Option value="25.0">Category Sponsor</option>
<Option value="10.0">Featured Sponsor</option>
<Option value="5.0">Text Link</option>
</select>
<select name="rate" onchange="updatePrice(this.form)">
<Option value="n/a">Choose Ad Period</option>
<Option value="12">12 months</option>
<Option value="11">11 months</option>
<Option value="10">10 months</option>
<Option value="9">9 months</option>
<Option value="8">8 months</option>
<Option value="7">7 months</option>
<Option value="6">6 months</option>
<Option value="5">5 months</option>
<Option value="4">4 months</option>
<Option value="3">3 months</option>
<Option value="2">2 months</option>
<Option value="1">1 months</option>
</select>
<input name="price" type="text" value="Please Choose Values"
</form>

</body>
</html

John

Jul 20 '05 #9
On Tue, 17 Feb 2004 11:55:05 -0600, John C
<Jo***********@mail.forum4designers.com> wrote:
I tried your code but ran into an error. I copied the code as you had
given it and it seems to run fine but as soon as you select an option
from either <select> menu it flags an error that says:

A Runtime error has occurred.

Line 7:
Error: 'time.value' is null or not an object.

Any suggestions? The code I entered is as follows;
[snip]
<form name="sponsor" method="post">
<select name="rate" onchange="updatePrice(this.form)">
<Option value="n/a">Choose Sponsorship</option> [...] </select>
<select name="rate" onchange="updatePrice(this.form)">
Should be: name="time"
<Option value="n/a">Choose Ad Period</option> [...] </select>


If you notice above, you changed the name of the second select element so
that it is the same as the first. Change it to "time" and there should be
no problems.

On an unrelated subject, you should really look into using a Usenet
service that is not Forum4Designers. Not only did they used to claim that
all material was theirs, when it actually belonged to the public medium
that is Usenet, they still do censor posts (complaints made in this group,
comp.lang.javascript, towards Forum4Designers have been deleted) and
destroy the formatting of posts. The code that I gave to you had actually
been indented to make it more readable, but their system condenses it to a
left-aligned mess. It has also been made clear recently that they do not
follow the posting standards that have been defined for sending messages
to Usenet. All three of your previous replies (1 to KC Wong, 2 to me) have
not been threaded by my newsreader due to badly constructed headers. I
also have three other replies from you that have no content, just a
quoting of the previous posts. This again is due to Forum4Designers' poor
design.

I would advise you to see if your ISP provides access to Usenet, and if
available, I would encourage you to use it. If not, use a better service
like Google Groups.

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #10
John C wrote:
Hi Mike,

<--snip-->

Any suggestions? The code I entered is as follows;


Yes.

Suggestion 1: Read the comp.lang.javascript FAQ.
Suggestion 2: Get a decent newsreader.
Suggestion 3: Read the comp.lang.javascript FAQ.
Suggestion 4: Learn to copy/paste properly.
Suggestion 5: Read the comp.lang.javascript FAQ.
Suggestion 6: Forget you ever heard of forum4webdesigners, its junk.
Suggestion 7: Read the comp.lang.javascript FAQ.

How many of my suggestions are you going to follow?

Oh yeah, Read the comp.lang.javascript FAQ.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/

Jul 20 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Anonymous | last post by:
Hi all, I have a form people use to enter checking data. One of the fields is calculated based on finding the difference of two input fields on the form. Here are the fields: CheckAmount...
1
by: Miguelito Bain | last post by:
hi everybody. i'm trying to find out what you can and can't do with/in calculated fields. any help, pointers, and/or advice would be greatly appreciated. i'm a newbie, but i want to learn, and...
5
by: John Bahran | last post by:
I am trying to use calculated fields in my query but all the results are zero ven when they're not. Please help. Thanks.
2
by: david | last post by:
Hi, I have a form with a couple of calculated fields. I want to put some code into the 'Form-Load' event to set various object states on the form, depending on the value of these fields. The...
2
by: Ryker | last post by:
I have a Purchase Order where I have a calculated field called Price that is calculated by multiplying Qty * Sales Price. I have 10 of these fields...Price 1 - Price 10 (for each line of the PO). ...
2
by: Olveres | last post by:
Hi, I have managed to work out how to add new lines into a calculated text box. However in this text box some of the outcome fields are empty however when previewing the report it includes the...
5
by: Henrik | last post by:
The problem is (using MS Access 2003) I am unable to retrieve long strings (255 chars) from calculated fields through a recordset. The data takes the trip in three phases: 1. A custom public...
2
by: jcf378 | last post by:
hi all. I have a form which contains a calculated control ("days") that outputs the # of days between two dates (DateDiff command between the fields and ). However, when I click "Filter by...
3
by: Matthew Wells | last post by:
If I have a calculated field that returns a value from a udf, does that field get calculated even when it's not being used in a Select statement? I knw that if a calculated field's formula is...
2
by: mkbrady | last post by:
I have a query that includes calculated fields that generate numeric results. I have wrapped conversion functions CLng() and CDdl() around the calculated fields to ensure the data types are...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.