Steph's Fahrenheit to Celsius Converter



<?php

include ('../header/assignmentHeader.php');
// code
error_reporting(E_ALL);    // set error reporting to all

echo('<h1>Steph\'s Fahrenheit to Celsius Converter</h1>');

// Note the variables being passed via the POST ternary operators.

$step = $_POST['step'] ?? 0;
$fahrenheit = $_POST['fahrenheit'] ?? '';

// filter all input taken from the Browser
$step = htmlentities($step);
$fahrenheit = htmlentities($fahrenheit);

if (
$step == 0)    // first time into this form
{
    
?>
    <link rel="stylesheet" href="../css/assignmentHeaderStyles.css" />
    <form action="selfCalculation.php" method="POST">
        <label for="name">Fahrenheit to Celsius</label>
        <input type="text" size="36" id="name" name="fahrenheit" 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 in a human readable json format
    
echo('</pre>');

    
$Celsius = ($fahrenheit - 32) / 1.8;

    echo(
"<h3>Celsius = $Celsius</h3>");

    
// provide a form to try again
    
?>

    <form action="selfCalculation.php" method="POST">
        <input type="hidden" name="step" value="0">
        <input type="submit" name="submit" value="Try Again">
    </form>

    <?php
}
show_source(__file__);
include(
'../footer/footer.php');
?>