Calculator
Posted: Wed Apr 29, 2009 11:54 pm
I am working on making a calculator, by combining html and php. The only thing that shows up is my html and my php doesnt work. I know it works as two separate files, but I am trying to get everything for this calculator to work in one file. The problem that I have when I test it, is that the results dont come out when I click the calculate button.
Code: Select all
<html>
<head>
<title>Calculator</title>
<script type="text/javascript">
function formReset()
{
document.getElementById("calculatorForm").reset()
}
</script>
</head>
<body bgcolor="maroon">
<form id="calculatorForm">
<h1><center>Calculator</center></h1>
<b><p>Please enter two (2) numbers where asked, choose your function (add, subtract, multiply, or divide), then click on the calculate button to get your calculations.</p></b>
<p align="center"><b>Enter the first number :</b><br/><input type="text" name="num1" size="20"><br>
<p align="center"><b>Enter the second number : </b><br/><input type="text" name="num2" size="20"></p>
</p>
<p align="center"><select name = "function">
<option value="+">Add [+]</option>
<option value="-">Subtract[-]</option>
<option value="*">Multiple[*]</option>
<option value="/">Divide[/]</option>
</select></p>
<p align="center"><input type="submit" value="Calculate" name="calculate"></p>
<p align="center"><input type="submit" value="clear" name="clear"></p>
<?php
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$func = $_POST['function'];
if (!$num1="" || !$num2="")
{
die("Value Cannot Be Null");
exit();
}
else
{
switch ($func) {
case "+":
$result = $_POST[num1] + $_POST[num2];
case "-":
$result = $_POST[num1] - $_POST[num2];
case "*":
$result = $_POST[num1] * $_POST[num2];
case "/":
$result = $_POST[num1] / $_POST[num2];
}
echo "Calculation result:" $result;
}
?>
</form>
</body>
</html>