Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!--Write a PHP script CalculateInterest.php which generates an HTML page that contains:
- An input text field to hold the amount of money; Radio buttons to choose the currency;
- An input text field to enter the compound annual interest amount;
- A dropdown menu to choose the period of time; A submit button.
- When the information is submitted, the script should print out the amount of money you will have after the selected period, rounded to 2 decimal places. Semantic HTML is required. Styling is not required.-->
- <!DOCTYPE html>
- <html>
- <head lang="en">
- <meta content="text/html" charset="UTF-8">
- <title>Calculate Interest</title>
- </head>
- <body>
- <form action="#" method="post">
- <p>
- <label for="startAmount">Enter Amount</label>
- <input type="text" name="startAmount" id="startAmount" autofocus required/>
- </p>
- <p>
- <input type="radio" name="currency" id="currency1" value="0" checked/><label
- for="currency1">USD</label>
- <input type="radio" name="currency" id="currency2" value="1"/><label
- for="currency2">EUR</label>
- <input type="radio" name="currency" id="currency3" value="2"/><label
- for="currency3">BGN</label>
- </p>
- <p>
- <label for="compound-interest">Compound Interest Amount</label>
- <input type="text" name="compound-interest" id="compound-interest"/>
- </p>
- <select name="periodOfTime" id="dropdown-list">
- <option value="0">3 Months</option>
- <option value="1">6 Months</option>
- <option value="2">1 Year</option>
- <option value="3">2 Years</option>
- <option value="4">3 Years</option>
- <option value="5">5 Years</option>
- </select>
- <input type="submit" value="Calculate"/>
- <?php
- mb_internal_encoding('UTF-8');
- if ($_POST && array_key_exists('startAmount', $_POST) && array_key_exists('currency', $_POST) &&
- array_key_exists('compound-interest', $_POST) && array_key_exists('periodOfTime', $_POST)
- ) {
- if (!preg_match("/^\d+\.?\d{0,2}$/", trim($_POST['startAmount']))) {
- exit('Wrong input!!! - The Amount must been integer or float-point number with 2 digits after decimal
- point!');
- }
- if (!preg_match("/^\d+\.?\d*$/", trim($_POST['compound-interest']))) {
- exit('Wrong input!!! - The Compound Interest Amount must been integer or float-point number!');
- }
- $amount = (float)$_POST['startAmount'];
- $currencyTypes = array('$', '€', 'ะปะฒ.');
- $currency = (array_key_exists($_POST['currency'], $currencyTypes)) ? $currencyTypes[$_POST['currency']] :
- null;
- $compoundInterest = (float)$_POST['compound-interest'];
- $months = array(3, 6, 12, 24, 36, 60);
- $timePeriod = (array_key_exists($_POST['periodOfTime'], $months)) ? $months[$_POST['periodOfTime']] :
- null;
- if (!$currency || !$timePeriod) {
- exit('Unexpected error!!! - Reload the Web Site again and carefully enter input fields!');
- }
- $compoundInterest = (($compoundInterest / 12) + 100) / 100;
- for ($i = 0; $i < $timePeriod; $i++) {
- $amount *= $compoundInterest;
- }
- $amount = round($amount, 2);
- echo ($_POST['currency'] == 2) ? $amount . ' ' . $currency : $currency . ' ' . $amount;
- }
- ?>
- </form>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement