Answer on Question#38968 – Charge calculation program – Programming, AJAX | JavaScript | HTML | PHP
Below are two independent solutions with same functionality written with PHP and Javascript.
PHP:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body>
<form id="charge" method="post" method="post">
Please input charge: <input id="charge" name="charge" yr="submit" submit=""/>
</form>
<?php
if (isset($_POST['charge'])) {
$charge = $_POST['charge'];
$tax = round($charge * 6.75 / 100, 2);
$tip = round(($charge + $tax) * 15 / 100, 2);
$total = $charge + $tax + $tip;
echo "meal charge: " . $_POST['charge'] . "<br>";
echo "tax amout: " . $tax . "<br>";
echo "tip: " . $tip . "<br>";
echo "<hr>";
echo "total: " . $total;
}
?>
</body>
</html>Javascript:
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=windows-1251" http-equiv="Content-Type">
<script>
window.onload = function() {
var charge = prompt("please enter charge");
var tax = (charge * 6.75 / 100).toFixed(2);
var tip = ((charge * 1 + tax * 1) * 15 / 100).toFixed(2);
var total = charge * 1 + tax * 1 + tip * 1;
var output = document.getElementById('result');
output.innerHTML = "charge: " + charge + "<br>" +
"tax:" + tax + "<br>" +
"tip: " + tip + "<br>" +
"total: " + total;
};
</script>
</head>
<body>
<div id="result">
</div>
</body>
</html>