Activity 4

1. Create a simple form with two input fields for the user to enter two numbers performs arithmetic operations on them. The form submits the data to a PHP script using GET method.

Solution:

Create two number input fields so that the user can input the desired numbers

Pass the numbers given to the PHP script to perform the operations

// Check if both variables 'name' and 'age' parameters are set in the URL
if (isset($_GET['firstNum']) && isset($_GET['secondNum'])) {
$fnum = $_GET['firstNum'];
$snum = $_GET['secondNum'];

// Display
echo "

First number: $fnum
Second number: $snum";
echo "

The sum of $fnum and $snum is " . $fnum + $snum . "

";
echo "

The difference of $fnum and $snum is ". $fnum - $snum . "

";
echo "

The product of $fnum and $snum is ". $fnum * $snum . "

";
echo "

The quotient of $fnum and $snum is ". $fnum / $snum . "

";
} else {
echo "

No parameters in the inputs. Please enter parameters then submit

";
}


Output:

Link to demo


2. Create a simple form that allows to input a message and counts the number of words in it. The form submits a data to a PHP script using the GET method.


Solution:

Create a textarea field so that the user can input a message. The message will then be passed to the PHP script.

//PHP script
if (isset($_GET['message']))
{
$message = $_GET['message'];
$wordCount = str_word_count($message);
echo "Message: " . $message . "<>";
echo "Word Count: " . $wordCount;
}
else{echo "Please enter your message and click submit to count the words in it.";}


Output:

Link to demo