I'm trying to calculate profit, which will be dynamically shown when user input other details. But not able to understand how exactly pass the inputs to the php function & then write it back to a form element. Here is what I've done till now.
<form action="invoice.php" method="get">
<input class="form-field" type="date" name="date" value="" placeholder="Date" id="date">
<input class="form-field" type="text" name="product_name" value=""placeholder="Product Name" id="product_name">
<input class="form-field" type="text" name="units" value="" placeholder="Product Unit" id="product_unit">
<input class="form-field" type="text" name="wholesale_price" value="" placeholder="Whole Sale Price" id="wholesale_price">
<input class="form-field" type="text" name="sell_price" value="" placeholder="Sell Price" id="sell_price">
<input class="form-field" type="text" name="" value="" placeholder="Profit" id="profit">
<script>
var units = parseFloat(document.getElementById("product_units"));
var wholesale_price = parseFloat(document.getElementById("wholesale_price"));
var sell_price = parseFloat(document.getElementById("sell_price"));
document.getElementById("profit").value = profit_calculation(units, wholesale_price, sell_price);
function profit_calculation(units, wholesale_price, sell_price) {
return (units * sell_price) - (units * wholesale_price);
}
</script>
</form>
& the invoice.php,
<?php
$unit = $_GET["units"];
$wholesale = $_GET["wholesale_price"];
$sell = $_GET["sell_price"];
function invoice_profit($units, $wholesale, $sell) {
return ($unit * $sell) - ($unit * $wholesale);
}
echo "Invoice #0001";
echo "<table border='1' align='center'>";
echo "<tr>";
echo "<td>" . $_GET["date"] . "</td>";
echo "<td>" . $_GET["product_name"] . "</td>";
echo "<td>" . $_GET["units"] . "</td>";
echo "<td>" . $_GET["wholesale_price"] . "</td>";
echo "<td>" . $_GET["sell_price"] . "</td>";
echo "<td>" . invoice_profit($units, $wholesale, $sell) . "</td>";
echo "</tr>";
echo "</table>"
?>
So basically, units, wholesale_price & sell_price will be passed to php function, profit will be calculated & written back to respective form id. Please help.
