AllThingsNetwork.org Logo

Security Simplified



Teaching Presentation - Constants

Video

Presentation Link

Code Example

Sales Tax Calculator

PHP and HTML Code

<!DOCTYPE html>
<!--
This is the website template for byui.allthingsnetwork.org
-->
<html lang="en-us">
<head>
    <title>AllThingsNetwork.org | Template</title>
</head>
<body>
<?php

// Define SALES_TAX Constant for 
define('SALES_TAX', 0.0825);

if(filter_input(INPUT_POST, 'action') == 'Submit'){
    // Collect the data sent from the form
    $cost = filter_input(INPUT_POST, 'cost');
}

?>
<h4>Sales Tax Calculator</h4>
<form method="post" action="<?php echo filter_input(INPUT_SERVER, 'PHP_SELF'); ?>" id="contactform">
    <fieldset id="contactform_input">
        <label for="cost">Total Cost: <input type="text" name="cost" id="cost" size="40" value="<?php if(isset($cost)) { echo $cost; } ?>" required></label>
        <label for="tax">Sales Tax: <input type="text" name="tax" id="tax" size="40" value="<?php echo SALES_TAX * 100 . "%"; ?>" disabled></label>
        <label for="price">Final Price: <input type="text" name="price" id="price" size="40" value="<?php if(isset($cost)) { echo "$" . $cost * (1 + SALES_TAX); } else { echo "$0.00"; } ?>" disabled></label>
    </fieldset>
    <fieldset id="contactform_submit">
        <input type='submit' name="action" id='action' value="Submit">
    </fieldset>
</form>
</body>
</html>