Don Alexander Eckford's Radius form
CODE FOLLOWS
<?php
include('includes/header.php');
// code
error_reporting(E_ALL); // set error reporting to all
echo('<h1>Don Alexander Eckford\'s Radius form</h1>');
// Note the variables being passed via the POST ternary operators.
// Retrieve and sanitize inputs
$step = filter_input(INPUT_POST, 'step', FILTER_SANITIZE_NUMBER_INT) ?? 0;
$radius = filter_input(INPUT_POST, 'radius', FILTER_SANITIZE_STRING) ?? '';
// Validate and ensure the radius is a valid number
if ($step == 1 && !is_numeric($radius)) {
echo('<p>Please enter a valid number for the radius.</p>');
$step = 0; // Reset step to show the form again
}
if ($step == 0) { // First time into this form or invalid input
?>
<form action="radius.php" method="POST">
<label for="radius">Radius</label>
<input type="text" size="36" id="radius" name="radius" value="">
<br><br>
<input type="hidden" name="step" value="1">
<input type="submit" name="submit" value="Submit">
</form>
<?php
} else { // Show the result if form was submitted successfully
echo('<p>The following is what the form gathered:</p>');
echo('<p>Please note: The POST array has been sanitized.</p>');
echo('<pre>');
print_r($_POST); // Output the sanitized $_POST array
echo('</pre>');
// Calculate the diameter
$diameter = $radius * 2;
echo("<h3>Diameter = " . htmlspecialchars($diameter) . "</h3>");
// Provide a form to try again
?>
<form action="radius.php" method="POST">
<input type="hidden" name="step" value="0">
<input type="submit" name="submit" value="Try Again">
</form>
<?php
}
include('includes/footer.php');
?>