473,385 Members | 1,154 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,385 software developers and data experts.

Real-time form-field updating

I have a form that is using HTML and PHP to make calculations for an
order form. One of the fields is asking for a total number of
workstations. I would like this field to be a real-time total of five
other fields in this form.

Is this possible?

Nov 23 '05 #1
7 2907
we*******@micfiles.com wrote:
I have a form that is using HTML and PHP to make calculations for an
order form. One of the fields is asking for a total number of
workstations. I would like this field to be a real-time total of five
other fields in this form.

Is this possible?


It is, use the `onkeyup' and `onkeypress' or the `onchange' event
handler. However, never ever trust data submitted with such a form
without server-side validation; the feature must not serve more
than the convenience of the form's user.
PointedEars
Nov 23 '05 #2
This is code from "http://www.mcfedries.com/JavaScript/AddItUp.asp"
that looks like it does what I am looking for, but it is based off of
drop down menus and I am using text boxes for my entry source. Can
anyone tell me how I need to edit this so that it will work for text
boxes?
<SCRIPT LANGUAGE="JavaScript">
<!--
function AddItUp(form) {
var item, firstValue, secondValue, thirdValue;

// Get the value of the item that's selected in the first list
item = form.first.selectedIndex;
firstValue = eval(form.first.options[item].value)

// Get the value of the item that's selected in the second list
item = form.second.selectedIndex;
secondValue = eval(form.second.options[item].value)

// Get the value of the item that's selected in the third list
item = form.third.selectedIndex;
thirdValue = eval(form.third.options[item].value)

// Add up the value and display the total
form.total.value = firstValue + secondValue + thirdValue
}
//-->
</SCRIPT>

Nov 23 '05 #3
we*******@micfiles.com wrote:
This is code from "http://www.mcfedries.com/JavaScript/AddItUp.asp"
The code you posted has many nonsensical traits. Do not trust this source.
that looks like it does what I am looking for, but it is based off of
drop down menus and I am using text boxes for my entry source. Can
anyone tell me how I need to edit this so that it will work for text
boxes?
<SCRIPT LANGUAGE="JavaScript">
In HTML4, the `language' attribute is deprecated, the `type' attribute is
required:

<script type="text/javascript">
<!--
Not needed and potentially harmful.
function AddItUp(form) {
var item, firstValue, secondValue, thirdValue;

// Get the value of the item that's selected in the first list
item = form.first.selectedIndex;
firstValue = eval(form.first.options[item].value)
var o = form.elements['first'];
firstValue = o.options[o.selectedIndex].value;
// Get the value of the item that's selected in the second list
item = form.second.selectedIndex;
secondValue = eval(form.second.options[item].value)
Same here.
// Get the value of the item that's selected in the third list
item = form.third.selectedIndex;
thirdValue = eval(form.third.options[item].value)
and here.
// Add up the value and display the total
form.total.value = firstValue + secondValue + thirdValue
} //-->
Same here.
</SCRIPT>


Corrected and applied for your use:

function addItUp(form)
{
var firstValue, secondValue, thirdValue;

// Get the value of the input box named/IDed "first"
firstValue = form.elements['first'].value;

// ...

// Add up the value and display the total
form.elements['total'].value = firstValue + secondValue + thirdValue;
}
HTH

PointedEars
Nov 23 '05 #4
Thank you... This is almost working now... I seem to have a syntax
issue in the addition part of the script... If my first variable gets
a value of 2 and my second variable gets a value of 4, my total should
be 6. However, I am currently getting a total of 24.

NOW what did I screw up?

Nov 23 '05 #5
Thank you both for your help! Once I got through your advice(s) and
remembered that coding is CaSe SeNsItIvE, I am up and running!

Thank you again, and I hope to be able to help out others with the
knowledge gained here!!

Nov 23 '05 #6
On 2005-11-21, we*******@micfiles.com <we*******@micfiles.com> wrote:
Thank you... This is almost working now... I seem to have a syntax
issue in the addition part of the script... If my first variable gets
a value of 2 and my second variable gets a value of 4, my total should
be 6. However, I am currently getting a total of 24.

NOW what did I screw up?


:) they come from the form as strings, one way to convet them to numbers is
to multiply them by 1.

--

Bye.
Jasen
Nov 24 '05 #7
JRS: In article <sl******************@clunker.homenet>, dated Wed, 23
Nov 2005 05:51:19, seen in news:comp.lang.javascript, Jasen Betts
<ja***@clunker.homenet> posted :
On 2005-11-21, we*******@micfiles.com <we*******@micfiles.com> wrote:
Thank you... This is almost working now... I seem to have a syntax
issue in the addition part of the script... If my first variable gets
a value of 2 and my second variable gets a value of 4, my total should
be 6. However, I am currently getting a total of 24.

NOW what did I screw up?

You screwed up your reading of FAQ 4.21.

:) they come from the form as strings, one way to convet them to numbers is
to multiply them by 1.


True. But many now prefer to convert a string to a number with the
unary + operator. FAQ 4.21.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Nov 26 '05 #8

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

Similar topics

4
by: Aaron W. West | last post by:
Timings... sometimes there are almost too many ways to do the same thing. The only significant findings I see from all the below timings is: 1) Integer math is generally fastest, naturally....
2
by: cwdjr | last post by:
Real One has a page to copy on their site that detects if the browser of a viewer of a page has Real One installed. The page is located at...
4
by: Allan Adler | last post by:
I'm trying to reinstall RedHat 7.1 Linux on a PC that was disabled when I tried to upgrade from RH7.1 to RH9. This is presenting lots of unexpected difficulties. Apart from wanting to keep the old...
10
by: Pavils Jurjans | last post by:
Hallo, It is know issue that due to the fact that computer has to store the real numbers in limited set of bytes, thus causing a minor imprecision from the decimal value that likely was stored....
17
by: David Scemama | last post by:
Hi, I'm writing a program using VB.NET that needs to communicate with a DOS Pascal program than cannot be modified. The communication channel is through some file databases, and I have a huge...
5
by: Henry Wu | last post by:
Hi, now that in .NET everything is on millimeter, I was wondering how can one convert Pixel to Millimeter and any user's screen/monitor. I saw the following code on how to convert pixel to...
0
by: support | last post by:
Veteran Real Estate Investor Shares some of his best Insider Secrets for successful investments! www.RealEstateBeginners.ws Have you ever wondered about investing in real estate? Maybe one...
12
by: Raymond Hettinger | last post by:
I am evaluating a request for an alternate version of itertools.izip() that has a None fill-in feature like the built-in map function: >>> map(None, 'abc', '12345') # demonstrate map's None...
16
by: DirtyHarry | last post by:
Good day everyone. This sounds like a stupid question, but I became just curious yesterday, and I looked up several textbooks. However, no textbooks on computer language (that I have ) mentioned...
2
by: Tim | last post by:
Folks, Can anyone thow some clarifying light on the following? I have come across a column with the same name and same data contents defined on different tables, on some the column is defined...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.