I have been working on this script not so much as to have it be a functional calculator, but rather to work on my PHP skills.
Anyways, I have a slight problem with it. First of all, here is the HTML code:
<html><head><title>Calculator</title></head>
<body>
<form action="calc.php" method="post">
<h3>Addition Calculator</h3>
<input type="text" name="add" />
+
<input type="text" name="ans1" />
<br />
<h3>Subtraction Calculator</h3>
<input type="text" name="sub" />
-
<input type="text" name="ans2" />
<br />
<h3>Multiplication Calculator</h3>
<input type="text" name="mul" />
*
<input type="text" name="ans3" />
<br />
<h3>Division Calculator</h3>
<input type="text" name="div" />
/
<input type="text" name="ans4" />
<br />
<input type="submit" name="submit" value="Calculate" />
<input type="reset" name="reset" value="Clear" />
</form>
</body>
</html>
And here is the PHP code:
<html><head><title>Results</title></head>
<?php
//Get info from addition fields
$add = $_POST['add'];
$ans = $_POST['ans1'];
//Calculate data using if/else statement
$answer11 = $add + $ans;
if ($add || $ans == "")
{
echo "";
} else {
echo "$add + $ans = $answer11";
}
//Get info from subtraction forms
$sub = $_POST['sub'];
$answer = $_POST['ans2'];
//Calculator data using if/else statement
$ans2 = $sub - $answer;
if ($sub || $answer =="")
{
echo "";
} else {
echo "$sub - $answer = $ans2";
}
//Get information from multiplication forms
$mul = $_POST['mul'];
$answer1 = $_POST['ans3'];
//Calculate data using if/else statement
$ans3 = $mul * $answer1;
if ($mul || $answer1 =="")
{
echo "";
} else {
echo "$mul * $answer1 = $ans3";
}
//Get information from division forms
$div = $_POST['div'];
$answer2 = $_POST['ans4'];
//Calculate data using if/else statement
$ans4 = $div / $answer2;
if ($div || $answer2 == "")
{
echo "";
} else {
echo "$div / $answer2 = $ans4";
}
//End PHP
?>
<br />
<input type="button" value="Back" onclick="window.location.href='http://firecat111.freehostia.com/java/index.php'">
</html>
I used the if/else statement so that if a user only typed in addition, the other 3 wouldn't show up as "0 / 0 = 0" or whatever. But now my problem is that whenever I type in a number in any of the fields, say 1+1, nothing shows up on the page. Did I do something wrong in the code? Point it out if you can please.
Also, one other question I have is this:
PHP must have an error or something when you divide by zero, because this always pops up:
Warning: Division by zero in /home/www/firecat111.freehostia.com/java/calc.php on line 50
Is there a code that can get rid of that error?