so hopefully i ask my question in the proper manner. I am trying to code a calculator that will display an error when someone tries to fill in a letterinstead of a number. How do i get this to work without a "Fatal error: Uncaught TypeError: Unsupported operand types: string string" stopping the code. And then the Exception should be shown next to the input field.
I add a screenshot of the result i am trying to get and i will give the code i have sofar.[
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculator</title>
</head>
<?php
$number1 = "";
$number2 = "";
$result = "";
$calculate = "";
if (isset($_GET['calculate'])) {
$number1 = $_GET['number1'];
$number2 = $_GET['number2'];
$calculate = $_GET['calculate'];
function isValidNumber1($number1)
{
if (!is_numeric($number1)) {
throw new Exception("Number 1 is not a number!");
}
}
try {
isValidNumber1($number1);
$result = $number1 $number2;
} catch (Exception $e) {
echo $e->getMessage() . PHP_EOL;
}
switch ($calculate) {
case "Add":
$result = $number1 $number2;
break;
case "Substract":
$result = $number1 - $number2;
break;
case "Multiply":
$result = $number1 * $number2;
break;
case "Divide":
$result = $number1 / $number2;
break;
case "Modulo":
$result = $number1 % $number2;
break;
}
}
?>
<body>
<h1>Calculator</h1>
<form action="" method="get" id="calculator">
<label for="number1">Number 1:</label>
<input type="text" name="number1" value="<?php echo $number1; ?>"><br><br>
<label for=" number2">Number 2:</label>
<input type="text" name="number2" value="<?php echo $number2; ?>"><br><br>
<?php
echo "<p>Operation: $calculate</p>";
echo "<p>Result: $result</p>";
?>
<div>
<input type="submit" name="calculate" value="Add">
<input type="submit" name="calculate" value="Substract">
<input type="submit" name="calculate" value="Multiply">
<input type="submit" name="calculate" value="Divide">
<input type="submit" name="calculate" value="Modulo">
</div>
</form>
</body>
</html>
CodePudding user response:
to fix the current implementation
- you need to add the
switch
statements inside thetry
block, - so when the validation exception fire it will escape running it
- and about showing the error beside the input you can set the
exception message
in avariable
and print it beside the input " ,
your implementation after those edits will looks like :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculator</title>
</head>
<?php
$number1 = "";
$number2 = "";
$result = "";
$calculate = "";
$number1Error = '';
if (isset($_GET['calculate'])) {
$number1 = $_GET['number1'];
$number2 = $_GET['number2'];
$calculate = $_GET['calculate'];
$errors = [];
function isValidNumber1($number1)
{
if (!is_numeric($number1)) {
throw new Exception("Number 1 is not a number!");
}
}
try {
isValidNumber1($number1);
$result = $number1 $number2;
switch ($calculate) {
case "Add":
var_dump($number1);
$result = $number1 $number2;
break;
case "Substract":
$result = $number1 - $number2;
break;
case "Multiply":
$result = $number1 * $number2;
break;
case "Divide":
$result = $number1 / $number2;
break;
case "Modulo":
$result = $number1 % $number2;
break;
}
} catch (Exception $e) {
$number1Error = $e->getMessage() . PHP_EOL;
}
}
?>
<body>
<h1>Calculator</h1>
<form action="" method="get" id="calculator">
<label for="number1">Number 1: </label>
<input type="text" name="number1" value="<?php echo $number1; ?>"> <?php echo $number1Error; ?><br><br>
<label for=" number2">Number 2:</label>
<input type="text" name="number2" value="<?php echo $number2; ?>"><br><br>
<?php
echo "<p>Operation: $calculate</p>";
echo "<p>Result: $result</p>";
?>
<div>
<input type="submit" name="calculate" value="Add">
<input type="submit" name="calculate" value="Substract">
<input type="submit" name="calculate" value="Multiply">
<input type="submit" name="calculate" value="Divide">
<input type="submit" name="calculate" value="Modulo">
</div>
</form>
</body>
</html>