Calculation Operators with Forms with Drop-down by Selecting Operators Addition, Subtraction, Multiplication, Division, Modules by Enter two numbers & selecting operators then result generated. see Demo Link
Program
<!DOCTYPE html>
<html>
<head>
<title>Two Numbers Calculation using Operators Using Forms</title>
</head>
<body>
<?php
$value1 = "";
$value2 = "";
$res = "";
if (isset($_POST["result"])) {
$value1 = $_POST['val_1'];
$value2 = $_POST['val_2'];
$operator = $_POST["operator"];
if (is_numeric($value1) && is_numeric($value2)) {
/*
1 : + Addition
2 : - Subtraction
3 : * Multiplication
4 : / Division
5 : % Modules
*/
switch ($operator) {
case '1' :
$add = ($value1 + $value2);
$res = "<h3>The Addition of $value1 and $value2 is $add </h3>";
break;
case '2' :
$sub = ($value1 < $value2) ? $res = "Not $value1 is Less Than $value2 " : ($value1 - $value2);
$res = "<h3>The Subtraction of $value1 - $value2 is $sub </h3>";
break;
case '3' :
$mul = ($value1 != 0 && $value2 != 0) ? ($value1 * $value2) : 'Not Multiply with 0';
$res = "<h3>The Multiply of $value1 * $value2 is $mul </h3>";
break;
case '4' :
$div = ($value1 != 0 && $value2 != 0) ? ($value1 / $value2) : 'Not Division with 0';
$res = "<h3>The Division of $value1 / $value2 is $div </h3>";
break;
case '5' :
$mol = ($value1 != 0 && $value2 != 0) ? ($value1 % $value2) : 'Not Modules with 0';
$res = "<h3>The Modules of $value1 and $value2 is $mol </h3>";
break;
default :
$res = "<h3>Select Operator </h3>";
break;
}
} else {
$res = "<h3>The Value is not in Numbers </h3>";
}
}
?>
<h2>Two Numbers Calculation using Operators Using Forms</h2>
<form method="POST">
<p> Enter First Value
<input type="text" name="val_1" value="<?php echo "$value1"; ?>" />
</p>
<p>Enter Second Value
<input type="text" name="val_2" value="<?php echo "$value2"; ?>"/>
</p>
<p>
<select name="operator">
<option> Select Operator </option>
<option value="1"> + Addition </option>
<option value="2"> - Subtraction </option>
<option value="3"> * Multiplication </option>
<option value="4"> / Division </option>
<option value="5"> % Modules </option>
</select> </p>
<input type="submit" name="result" value="Result">
</form>
<?php
echo $res;
?>
</body>
</html>Demo
(Visited 234 times, 1 visits today)
Written by:
