html - Calculator using switch case in PHP
Get the solution ↓↓↓Solution:
1) You have given {{-code-9}code{-code-9}7}mit value{{{{-code-9}code{-code-9}4}code{{-code-9}code{-code-9}4}2}code{{{-code-9}code{-code-9}4}code{{-code-9}code{-code-9}4}2}1}
&{{{-code-9}code{-code-9}4}code{{-code-9}code{-code-9}4}2}
.
2) Whenever you {{-code-9}code{-code-9}7}mit your form it takes the value{{{{-code-9}code{-code-9}4}code{{-code-9}code{-code-9}4}2}code{{{-code-9}code{-code-9}4}code{{-code-9}code{-code-9}4}2}1}
&{{{-code-9}code{-code-9}4}code{{-code-9}code{-code-9}4}2}
in{{-code-9}code{-code-9}5}
value.
3) In switch case you mentioned cases :{{-code-9}code{-code-9}6}
&{{-code-9}code{-code-9}7}
, where your post value having{{{{-code-9}code{-code-9}4}code{{-code-9}code{-code-9}4}2}code{{{-code-9}code{-code-9}4}code{{-code-9}code{-code-9}4}2}1}
&{{{-code-9}code{-code-9}4}code{{-code-9}code{-code-9}4}2}
. Which not satisfying any cases.
4) Just replace your{{{{-code-9}code{-code-9}4}code{{-code-9}code{-code-9}4}2}code{{{-code-9}code{-code-9}4}code{{-code-9}code{-code-9}4}2}1}
&{{{-code-9}code{-code-9}4}code{{-code-9}code{-code-9}4}2}
with{{-code-9}code{-code-9}6}
&{{-code-9}code{-code-9}7}
respectively in your form input value like this
<input type="{{-code-9}code{-code-9}7}mit" name="{{-code-9}code{-code-9}7}mit" value="{{-code-9}code{-code-9}6}">
<input type="{{-code-9}code{-code-9}7}mit" name="{{-code-9}code{-code-9}7}mit" value="{{-code-9}code{-code-9}7}">
Answer
Solution:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<?php
$num1 = 0;
$num2 = 0;
$calc = 0;
if (isset($_POST['submit']))
{
$num1 = $_POST['n1'];
$num2 = $_POST['n2'];
$calc = calculate($num1, $num2, $_POST['submit']);
}
function calculate($num1,$num2,$op)
{
$calc = 0;
switch ($op)
{
case '+':
$calc = $num1 + $num2;
break;
case '-':
$calc = $num1 - $num2;
break;
}
return $calc;
}
?>
</head>
<body>
<form action="" method="post">
NO 1 : <input name='n1' value="<?php echo $num1;?>">
<br><br>
NO 2: <input name='n2' value="<?php echo $num2;?>"><br><br>
total: <input name="res" value="<?php echo $calc;?>"><br><br>
<input type="submit" name="submit" value="+">
<input type="submit" name="submit" value="-">
</form>
</body>
</html>
Answer
Solution:
You have lot of bugs in your code. like
1) You have declared function but never call it.
2) Variable names issue.
3) Submit button value and switch case never match etc...
You need to change your code as below:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<?php
$num1 = "";
$num2 = "";
$calc = "";
if (isset($_POST['submit']))
{
$num1 = $_POST['n1'];
$num2 = $_POST['n2'];
function calculate($num1,$num2){
$calc = "";
switch ($_POST['submit']) {
case 'addition':
$calc = $num1 + $num2;
break;
case 'sub':
$calc = $num1 - $num2;
break;
}
return $calc;
}
$calc = calculate($num1,$num2);
}
?>
</head>
<body>
<form action="" method="post">
NO 1 : <input name='n1' value="<?php echo $num1;?>">
<br><br>
NO 2: <input name='n2' value="<?php echo $num2?>"><br><br>
total: <input type="res" value="<?php echo $calc;?>"><br><br>
<input type="submit" name="submit" value="addition">
<input type="submit" name="submit" value="sub">
</form>
</body>
</html>
Answer
Solution:
You are passing "+" and "-" into the switch and the case is "addition" and "sub". They are not matching.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: 403 this action is unauthorized.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.