Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Add user inputs

Posted on 2004-10-12
5
Medium Priority
?
235 Views
Last Modified: 2010-04-06
Hey, i know that this is a simple question, but i cant find the answer it ANYWHERE.

I want to multiply and add numbers that a user types into a form.  It is going to be used so that users can calculate costs.

Please give examples of codes along with you solutions

Thanks!!!
0
Comment
Question by:kylen1010
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 1

Expert Comment

by:apg88
ID: 12294110
Here is the code for the calculator with a fixed item and price.
<html>
<head>
<script language="JavaScript">
<!-- hide this script from old browsers
function temp(form)
{
  var f = parseFloat(form.NumberOfItems.value, 10);
  var c = 0;
  var p = 12.32;      //This is the actual pice of the item
  c = "$" + f*p;
  form.TotalPrice.value = c;
}
// done hiding from old browsers -->
</script>
</head>
<body>
<FORM>
<h2>Price Calc</h2>
<p>Items are @ $12.32 each.</p>
<p>Enter a the number of items:
  <INPUT NAME="NumberOfItems" VALUE="1" MAXLENGTH="15" SIZE=15>
</p>
<p>
Click this button to the final price.:
    <INPUT NAME="calc" VALUE="Calculate" TYPE=BUTTON
onClick=temp(this.form)>
<p>
The total price is:
    <INPUT NAME="TotalPrice" READONLY SIZE=15>
</FORM>
</body>
</html>
0
 
LVL 2

Author Comment

by:kylen1010
ID: 12303008
Thanks!  I am trying to modify the script just a little (I have very little knowlege in Javascript) The total amount (var z) that i get is correct, however there are 00000000's at the end of the answer.  I was wondering if there was a way to remove those (so the answer is 0.06 instead of 0.06000000, for example)

<html>
<head>
<script language="JavaScript">
<!-- hide this script from old browsers
function temp(form)
{
  var a = parseFloat(form.paper.value, 10);
  var b = 0.05;

  var c = parseFloat(form.ink.value, 10);
  var d = 0.01

  var z = 0;
  var x = 0;
  var y = 0;

  x = a*b;
  y = a*d;
  z = $ + x + y
  form.TotalPrice.value = z;
}
// done hiding from old browsers -->
</script>
</head>
<body>
<FORM>
<h2>Price Calc</h2>

<p>Paper ($0.05/page):
  <INPUT NAME="paper" VALUE="1" MAXLENGTH="15" SIZE=15>
</p>
<p>Ink ($0.01/page):
  <INPUT NAME="ink" VALUE="1" MAXLENGTH="15" SIZE=15>
</p>
<p>
Click this button to the final price.:
    <INPUT NAME="calc" VALUE="Calculate" TYPE=BUTTON
onClick=temp(this.form)>
<p>
The total price is:
    <INPUT NAME="TotalPrice" READONLY SIZE=15>
</FORM>
</body>
</html>


Thanks again!
0
 
LVL 2

Author Comment

by:kylen1010
ID: 12303107
I know that I could just do:

  var a = parseFloat(form.paper.value, 10);
  var b = 0.06;

  var z = 0;
  var x = 0;
  var y = 0;

  x = a*b;
  z = "$" + x

but i was just wondering if i could remove the 0's using the script in my previous post.

Thanks
0
 
LVL 1

Accepted Solution

by:
apg88 earned 100 total points
ID: 12304748
Here you go:

<html>
<head>
<script language="JavaScript">
<!-- hide this script from old browsers
function temp(form)
{
  var a = parseFloat(form.paper.value, 10);
  var b = 0.05;

  var c = parseFloat(form.ink.value, 10);
  var d = 0.01;

  var z = 0;
  var x = 0;
  var y = 0;
  var w = 0;

  x = Math.round(a*b*100)/100;
  y = Math.round(c*d*100)/100;
  w = Math.round((x+y)*100)/100;
  z = "$" + w;
  form.TotalPrice.value = z;
}
// done hiding from old browsers -->
</script>
</head>
<body>
<FORM>
<h2>Price Calc</h2>

<p>Paper ($0.05/page):
  <INPUT NAME="paper" VALUE="1" MAXLENGTH="15" SIZE=15>
</p>
<p>Ink ($0.01/page):
  <INPUT NAME="ink" VALUE="1" MAXLENGTH="15" SIZE=15>
</p>
<p>
Click this button to the final price.:
    <INPUT NAME="calc" VALUE="Calculate" TYPE=BUTTON
onClick=temp(this.form)>
<p>
The total price is:
    <INPUT NAME="TotalPrice" READONLY SIZE=15>
</FORM>
</body>
</html>
0
 
LVL 2

Author Comment

by:kylen1010
ID: 12313385
Great! THANKS A MILLION!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question