By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,720 Members | 2,237 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,720 IT Pros & Developers. It's quick & easy.

Real-time form-field updating

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.