Click to See Complete Forum and Search --> : Problems with my tip Generator


AcRoMaN
12-19-2003, 06:59 AM
I can't figure out how to write this so it works! what I am trying to do is have the script tell you what 15% of the input value is!
Here is what I have so far!


<html>
<head>
<title>Tip</title>
<center>
<br>
Enter number to see what a 15% tip would be!
<br>
<script language="javascript">
var TipAmount;
amount="money"
TipAmount=amount*.15;
document.write ("A 15% tip for $",amount," would be: $"TipAmount");
</script></center>
</head>
<body>
<center><FORM NAME="form" METHOD="GET" ACTION="#">
<br>$ <INPUT TYPE="TEXT" NAME="money" SIZE="10">
<br>
<INPUT TYPE="SUBMIT" NAME="total" VALUE="Enter">
</FORM></center>
</body>
</html>


The Problem that occurs when I use this is, when I submit the number it returns to a the blank default it started with and does not write what 15% of the input value is! Please Help! I can get it to work if I erase the body and replace money with a number! but I want the user to be able to type in the amount they want! Please Help!

Thanks,
Jordan

TheBearMay
12-19-2003, 07:19 AM
If you just want to place it on the page you might want to consider using a DIV and setting the innerHTML property:


<html>
<head>
<title>Tip</title>
<center>
<br>
Enter number to see what a 15% tip would be!
<br>
<script language="javascript">
var TipAmount;
amount="money"
TipAmount=amount*.15;
dispDiv.innerHTML="A 15% tip for $"+amount+" would be: $"+TipAmount");
</script></center>
</head>
<body>
<center><FORM NAME="form" METHOD="GET" ACTION="#">
<br>$ <INPUT TYPE="TEXT" NAME="money" SIZE="10">
<br>
<INPUT TYPE="SUBMIT" NAME="total" VALUE="Enter">
</FORM></center>
<div id=dispDiv></div>
</body>
</html>

Pittimann
12-19-2003, 07:30 AM
Hi!

To TheBearMay: I think your idea to use a div where to display the result is allright! AcRoMaN's main problem is somewhere else: amount's value is "money" and "money" * 0.15 is not anumber. Besides that, the quotes are not correct.

Here's an example (without using innerHTML) document.writing the (unrounded) tip after the button is pressed:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Tip</title>
<script language="JavaScript" type="text/javascript">
function showTip(){
var TipAmount;
amount=document.form.money.value;
TipAmount=amount*.15;
document.write ("A 15% tip for $"+amount+" would be: $"+Math.round(TipAmount,2));
}
</script>
</head>
<body>
<center>
<br>
Enter number to see what a 15% tip would be!
<br>
<FORM NAME="form" METHOD="GET" ACTION="#">
<br>$ <INPUT TYPE="TEXT" NAME="money" SIZE="10">
<br>
<INPUT TYPE="SUBMIT" NAME="total" VALUE="Enter" onclick="showTip()">
</FORM></center>
</body>
</html>

Cheers - Pit

AcRoMaN
12-19-2003, 07:31 AM
It is still not working properly it is doing the same thing with the blank default form loading! Please remember I'm Very new this is the first script I've tried writing by myself!

requestcode
12-19-2003, 07:36 AM
Here is a modification of your code:
<html>
<head>
<title>Tip</title>
<script language="javascript">
function Calctip()
{
if(isNaN(document.tipform.money.value)) // insure numbers only
{
alert("Please enter numbers only!")
document.tipform.money.value="0.00"
}
else
{
TipAmount=(Math.round((document.tipform.money.value*.15) * Math.pow(10,2)))/100
document.tipform.money.value=TipAmount
}
}

</script>
</head>
<body>
Enter number to see what a 15% tip would be!
<center><FORM NAME="tipform">
<br>$ <INPUT TYPE="TEXT" NAME="money" SIZE="10" value="0.00">
<br>
<INPUT TYPE="button" NAME="total" VALUE="Calculate Tip" onClick="Calctip()">
</FORM></center>
</body>
</html>

AcRoMaN
12-19-2003, 07:43 AM
Thank you so Much! :D

Kor
12-19-2003, 07:50 AM
Ok, if it is your first script, take good care about the variables and parameters. In javascript variables are untyped, so you have to take care when you declare them as numbers or as strings. There are some methods to "extract" numbers from a string but it is a better way to have a correct definition.