Problem 1
Create a simple form with two input fields for the user to enter two numbers performs
arithmetic operations
Solution:
Passing
<form method="get" action="activity4handle.php">
<label for="num1">Enter 1st number: </label>
<input type="number" name="n1" id="n1">
<label for="num2">Enter 2nd number: </label>
<input type="number" name="n2" id="n2">
<input type="submit" name="add" value="Addition" id="submit">
<input type="submit" name="subtract" value="Subtraction" id="submit">
<input type="submit" name="multiply" value="Multiplication" id="submit">
<input type="submit" name="divide" value="Division" id="submit">
</form>
Handling
<?php
$string;
if(isset($_GET['add'])){
$result = $num1 + $num2;
$string = "$num1 + $num2 = $result";
}elseif(isset($_GET['subtract'])){
$result = $num1 - $num2;
$string = "$num1 - $num2 = $result";
}elseif(isset($_GET['multiply'])){
$result = $num1 * $num2;
$string = "$num1 * $num2 = $result";
}elseif(isset($_GET['divide'])){
$result = $num1 / $num2;
$string = "$num1 / $num2 = $result";
}
echo <h1>$string h1>;
?>
Problem 2
Create a simple form that allows to input a message and counts the number of words in it.
Solution
Passing
<form action="4-2Handle.php" method="get">
<label for="msg">Input Message: </label>
<input type="text" name="message">
<input type="submit" value="Count the number of words" id="sub2">
<form>
Handling
<?php
if(isset($_GET['message'])){
$message = $_GET['message'];
}
$result = explode(" ",$message);
$new = count($result);
echo "<h1 class='haq'>The word/s in the message are $new. h1>";
?>