I have 4 radio buttons +
, -
, *
, /
and I am planning to do if I select +
it will add the result of the two textbox and display it to the 3rd texbox and so if I select -
sign it will minus the two texbox and display it to the 3rd textbox and so on. Any help with this would be appreciated. this is the picture
<html>
<body>
<?php
function add($x,$y){
$add=$x + $y;
return $add;
}
function subtract($x,$y){
$subtract=$x - $y;
return $subtract;
}
function multiply($x,$y){
$multiply=$x * $y;
return $multiply;
}
function divide($x,$y){
$divide=$x / $y;
return $divide;
}
?>
<form action="#" method="post">
Num1:<input name="num1"><br>
<input type='radio' name='express' value='add'>+
<input type='radio' name='express' value='subtract'>-
<input type='radio' name='express' value='multiply'>*
<input type='radio' name='express' value='divide'>/
<br>
Num2:<input name="num2">
Result<input value='<?php
?>'>
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST['submit'])){
$x=$_POST['num1'];
$y=$_POST['num2'];
$z=$_POST['express'];
if($z == 'add'){
echo add($x,$y);
}
else if($z=='subtract'){
echo subtract($x,$y);
}
else if($z=='multiply'){
echo multiply($x,$y);
}
else{
echo divide($x,$y);
}
}
?>
</body>
</html>
<html>
<body>
<?php
$Result = '';
if(isset($_POST['submit'])){
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$express = $_POST['express'];
switch ($express) {
case 'add':
$Result = $num1 + $num2;
break;
case 'subtract':
$Result = $num1 - $num2;
break;
case 'multiply':
$Result = $num1 * $num2;
break;
case 'divide':
if(0 != $num2 ){
$Result = $num1 / $num2;
}
break;
default:
break;
}
}
?>
<form action="#" method="post">
Num1:<input name="num1"><br>
<input type='radio' name='express' value='add'>+
<input type='radio' name='express' value='subtract'>-
<input type='radio' name='express' value='multiply'>*
<input type='radio' name='express' value='divide'>/
<br>
Num2:<input name="num2">
Result<input value='<?php echo $Result;?>'>
<input type="submit" name="submit">
</form>
</body>
</html>
I suggest you achieve it by using JavaScript and separate the PHP code from HTML code.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments