Simple Calculator

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tmattevc
Forum Newbie
Posts: 1
Joined: Wed Dec 17, 2008 11:35 pm

Simple Calculator

Post by tmattevc »

So i'm a beginner and trying to figure out something that seems like it should be really easy:

I want someone to enter an amount in a text field, an amount in another text, multiply those numbers and multiply that by .2 and then post the result into another text field.

Months (text field)
Rent (text field)
Calculate (button)
Total (text field to display result of months*rent*.2)

Here is basic page:

<html>
<head>
</head>
<body>
<form method="post" action="handler.php">
<p>Months <input name="months" type="text" id="months"></p>
<p>Rent <input name="rent" type="text" id="rent"></p>
<p><input type="submit" name="Submit" value="Calculate"></p>
<p>Total<input name="total" type="text" id="total"></p>
</form>
</body>
</html>

This is what i would assume would handle this (handler.php):

<html>
<head>
</head>
<body>
<?php
$answer = $months * $rent * .2;
echo "$answer"; ?>
</body>
</html>

I'm sure this can be really simple but my knowledge is pretty limited.
Any help? also this (if it was right) would print it on a separate page rather than displaying it in the text field (Total)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Simple Calculator

Post by requinix »

Where are $months and $rent coming from? PHP doesn't define those automatically: how should it know what values they should get?

Take a look at how to handle form input.

Code: Select all

<html>
<head>
</head>
<body>
<?php
$answer = $_POST["months"] * $_POST["rent"] * .2;
echo $answer; ?>
</body>
</html>
Post Reply