I’m starting to learn php and running into an error with some of my code. Im working on a simple calculator and getting an error message from xdebug “Undefined index”.
When i input some numbers into the input fields the error goes away.
I’m running a PHP server locally through a program called “Uniform Server” running the latest PHP and Xdebug installed locally also.
Here is the error:
And the code I’m using:
<?php
class Calculator
{
public function Addition($num1, $num2)
{
return $num1 + $num2;
}
public function Subtraction($num1, $num2)
{
return $num1 - $num2;
}
public function Multiplication($num1, $num2)
{
return $num1 * $num2;
}
public function Division($num1, $num2)
{
return $num1 / $num2;
}
}
$calculator = new Calculator();
$choice = $_POST['choice'];
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
if(isset($_POST['submit']))
{
if(is_numeric($num1) && is_numeric($num2))
{
switch($choice)
{
case "addition":
echo "<h3>" . $num1 . " + " . $num2 . " = " . $calculator->Addition($num1,$num2) . "</h3>";
break;
case "subtraction":
echo "<h3>" . $num1 . " - " . $num2 . " = " . $calculator->Subtraction($num1,$num2) . "</h3>";
break;
case "Multiplication":
echo "<h3>" . $num1 . " * " . $num2 . " = " . $calculator->Multiplication($num1,$num2) . "</h3>";
break;
case "division":
echo "<h3>" . $num1 . " / " . $num2 . " = " . $calculator->Division($num1,$num2) . "</h3>";
break;
default:
echo "Please pick a choice";
}
}
else
{
echo "Please enter only numeric numbers";
}
}
?>
<html>
<head>
<title>Simple Calculator</title>
</head>
<body>
<h1>Simple Calculator</h1>
<form action="index.php" method="POST">
<table>
<tr>
<td>First Number: <input type="text" name="num1" /></td>
</tr>
<tr>
<td>Second Number: <input type="text" name="num2" /></td>
</tr>
<tr>
<td>Select A choice</td>
</tr>
<tr>
<td>Addition<input type="radio" name="choice" value="addition" /></td>
</tr>
<tr>
<td>Subtraction<input type="radio" name="choice" value="subtraction"/></td>
</tr>
<tr>
<td>Multiplication<input type="radio" name="choice" value="multiplication" /></td>
</tr>
<tr>
<td>Division<input type="radio" name="division" /></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>
I appreciate any help anyone can give me.