Home
Austin's Pythagorean Theorem Form
Code follows
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Pythagorean Theorem</title>
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<div class="page">
<p class="nav"><a href="../index.php">Home</a></p>
<?php
include('includes/header.php');
// code
error_reporting(E_ALL); // set error reporting to all
echo('<h1>Austin\'s Pythagorean Theorem Form</h1>');
// Note the variables being passed via the POST ternary operators.
$step = isset($_POST['step']) ? $_POST['step'] : 0;
$sideA = isset($_POST['sideA']) ? $_POST['sideA'] : '';
$sideB = isset($_POST['sideB']) ? $_POST['sideB'] : '';
// filter all input taken from the Browser
$step = htmlentities($step);
$sideA = htmlentities($sideA);
$sideB = htmlentities($sideB);
if ($step == 0) // first time into this form
{
?>
<form action="pythagorean-theorem.php" method="POST">
<label for="name">Side A</label>
<input type="text" size="36" id="sideA" name="sideA" value="">
<label for="name">Side B</label>
<input type="text" size="36" id="sideB" name="sideB" value="">
<br><br>
<input type="hidden" name="step" value="1">
<input type="submit" name="submit" value="Submit">
</form>
<?php
} else // else show what the form gathered
{
echo('<p>The following is what the form gathered</p>');
echo('<pre>');
print_r($_POST); // note this dumps the entire contents of the $_POST array
echo('</pre>');
$sideASquared = $sideA ** 2;
$sideBSquared = $sideB ** 2;
$sides = $sideASquared + $sideBSquared;
$hypotenuse = sqrt($sides);
echo("<h3>The Hypotenuse is = $hypotenuse</h3>");
// provide a form to try again
?>
<form action="pythagorean-theorem.php" method="POST">
<input type="hidden" name="step" value="0">
<input type="submit" name="submit" value="Try Again">
</form>
<?php
}
include('includes/footer.php');
?>
<br>
<h2>Code follows</h2>
<div class="code"><?php highlight_file(__FILE__); ?></div>
<p class="nav nav-foot"><a href="../index.php">Home</a></p>
</div>
</body>
</html>