Starter: Simple Calculations on a Form

J

John Ciccone

I must be the stupidest guy on the planet. I just want to know how to work
with a form so that values entered into fields are used to calculate an
answer. Believe it or not, I have programmed in Visual Basic etc., but have
no idea where to begin when it comes to HTML.

I've tried article 319308, but get the error: "Method Not Allowed
The requested method POST is not allowed or the URL /send.asp."

I've looked through a zillion pages of Add Ins and Templates and can't seem
to find a place to start.

I'm happy to be pointed to examples, tutorials, anything. Just to get started.

FP2000
 
T

Trevor L.

John said:
I must be the stupidest guy on the planet. I just want to know how to
work with a form so that values entered into fields are used to
calculate an answer. Believe it or not, I have programmed in Visual
Basic etc., but have no idea where to begin when it comes to HTML.

I've tried article 319308, but get the error: "Method Not Allowed
The requested method POST is not allowed or the URL /send.asp."

I've looked through a zillion pages of Add Ins and Templates and
can't seem to find a place to start.

I'm happy to be pointed to examples, tutorials, anything. Just to get
started.

FP2000

Here is a simple example. Clicking Check will place 9.70873786407767 [ 10
*100 / (10 +3) ] in the box D. This is from another request, and is probably
not typical. You can alter the calculation to whatever you wnat

<html>
<head>
<script type="text/javascript">
function sendform()
{
with (document.form1)
{ result.value = +val1.value * 100 / (val1.value +
2.value) }
}
</script>
</head>
<body>

<form name="form1" action="">
<table>
<tr>
<td>A: </td>
<td><input type="text" id="val1" value="10"/></td>
</tr>
<tr>
<td>B: </td>
<td><input type="text" id="val2" value="3" /></td>
</tr>
<tr>
<td>D: </td>
<td><input type="text" id="result" size="20" /></td>
</tr>
</table>
</form>
<input type="button" id="Check" value="Check" onclick="sendform()" />

</body>
</html>

--
Cheers,
Trevor L.
[ Microsoft MVP - FrontPage ]
MVPS Website: http://trevorl.mvps.org/
----------------------------------------
 
J

John Ciccone

Thank you, Trevor. I get an error:

Line: 10 Character: 3 Error: Expected ')'

Any idea what that might be?

Trevor L. said:
John said:
I must be the stupidest guy on the planet. I just want to know how to
work with a form so that values entered into fields are used to
calculate an answer. Believe it or not, I have programmed in Visual
Basic etc., but have no idea where to begin when it comes to HTML.

I've tried article 319308, but get the error: "Method Not Allowed
The requested method POST is not allowed or the URL /send.asp."

I've looked through a zillion pages of Add Ins and Templates and
can't seem to find a place to start.

I'm happy to be pointed to examples, tutorials, anything. Just to get
started.

FP2000

Here is a simple example. Clicking Check will place 9.70873786407767 [ 10
*100 / (10 +3) ] in the box D. This is from another request, and is probably
not typical. You can alter the calculation to whatever you wnat

<html>
<head>
<script type="text/javascript">
function sendform()
{
with (document.form1)
{ result.value = +val1.value * 100 / (val1.value +
2.value) }
}
</script>
</head>
<body>

<form name="form1" action="">
<table>
<tr>
<td>A: </td>
<td><input type="text" id="val1" value="10"/></td>
</tr>
<tr>
<td>B: </td>
<td><input type="text" id="val2" value="3" /></td>
</tr>
<tr>
<td>D: </td>
<td><input type="text" id="result" size="20" /></td>
</tr>
</table>
</form>
<input type="button" id="Check" value="Check" onclick="sendform()" />

</body>
</html>

--
Cheers,
Trevor L.
[ Microsoft MVP - FrontPage ]
MVPS Website: http://trevorl.mvps.org/
 
J

John Ciccone

Me again... actually I just changed the function and it's got me going. Thanks!

One thing is that if I enter:

{ result.value = val1.value + val2.value }

The result is obviously concatenating the digits as text. How do I 'add' the
two values? (I've actually figured out multiply and divide. Yeehaw!)
 
J

Jon Spivey

Hi,
You'd need to cast them as numbers, either
parseFloat(val1.value) for decimals
or
parseInt(val1.value) for integers

Cheers,
Jon
 
T

Trevor L.

John said:
Thank you Jon and Trevor!

John,
Sorry that my code didn't work (it had been tested) and that I didn't get
back to you (time difference here in Australia)

Thanks Jon
--
Cheers,
Trevor L.
[ Microsoft MVP - FrontPage ]
MVPS Website: http://trevorl.mvps.org/
----------------------------------------
 

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

Top