[Solved] Sum two (2) numbers in PHP with HTML input form

  

3
Topic starter
  1. Write a PHP script to sum two numbers.
  2. The script should display HTML form to enter the numbers.
  3. If the numbers are passed as parameters, display their sum.
1 Answer
2

Here's my solution:

<!DOCTYPE html>
<html>
<head>
    <title>Sum</title>
</head>
<body>
 
<form>
    <div>Number 1:</div>
    <input type="number" name="num1"/>
    <div>Number 2:</div>
    <input type="number" name="num2"/>
    <div><br><input type="submit" value="CHECK THE SUM"></div><br>
</form>
 
<?php
if (isset($_GET['num1']) && isset($_GET['num2'])) {
 
    $num1 = $_GET['num1'];
    $num2 = $_GET['num2'];
    $sum = $num1 + $num2;
    echo $num1 . " + " . $num2 . " = " . $sum;
}
?>
 
</body>
</html>
Share: