<FORM action="info.php" method="post">
<P>
<LABEL for="firstNum">First Number: </LABEL>
<INPUT type="text" name="firstNum" id="firstNumberLabel"><BR>
<LABEL for="secondNum">Second Number: </LABEL>
<INPUT type="text" name="secondNum" id="secondNumberlabel"><BR>
<INPUT type="radio" name="calc" value="1"> Add<BR>
<INPUT type="radio" name="calc" value="2"> Subtract<BR>
<INPUT type="radio" name="calc" value="3"> Multiply<BR>
<INPUT type="radio" name="calc" value="4"> Divide<BR>
<INPUT type="submit" value="Send">
<INPUT type="reset">
</P>
</FORM>
<form action= <?php echo $_SERVER['PHP_SELF'] ?> method="post">
<label for="text_field">First Number: </LABEL>
<input type="text" name="firstNum" id="text_field"><BR>
<label for="text_field2">Second Number: </LABEL>
<input type="text" name="secondNum" id="text_field2"><BR>
<INPUT type="radio" name="calc" value="1"> Add<BR>
<INPUT type="radio" name="calc" value="2"> Subtract<BR>
<INPUT type="radio" name="calc" value="3"> Multiply<BR>
<INPUT type="radio" name="calc" value="4"> Divide<BR>
<INPUT type="submit" value="Send">
</form>
<?php
switch ($_POST["calc"]){
case "1":
echo "Result: ", $_POST['firstNum']+$_POST['secondNum'], "<br>";
break;
case "2":
echo "Result: ", $_POST['firstNum']-$_POST['secondNum'], "<br>";
break;
case "3":
echo "Result: ", $_POST['firstNum']*$_POST['secondNum'], "<br>";
break;
case "4":
echo "Result: ", $_POST['firstNum']/$_POST['secondNum'], "<br>";
break;
default:
break;
}
?>
and with the same info.php as before
<?php
switch ($_POST["calc"]){
case "1":
echo "Result: ", $_POST['firstNum']+$_POST['secondNum'], "<br>";
break;
case "2":
echo "Result: ", $_POST['firstNum']-$_POST['secondNum'], "<br>";
break;
case "3":
echo "Result: ", $_POST['firstNum']*$_POST['secondNum'], "<br>";
break;
case "4":
echo "Result: ", $_POST['firstNum']/$_POST['secondNum'], "<br>";
break;
default:
break;
}
?>
the problem with it is i think there is quite a lot of repeated code
solved How to ‘$_POST’ back to the same page or to a different result page in php? [closed]