我正在尝试计算利润,这将在用户输入其他详细信息时动态显示。但无法理解如何将输入完全传递给php函数&然后将其写回表单元素。这是我到目前为止所做的。
<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>
&安培; 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>"
?>
所以基本上,单位,wholesale_price&amp; sell_price将传递给php函数,利润将被计算和&amp;写回各自的表格ID。 请帮忙。
答案 0 :(得分:1)
您需要ajax将发票从服务器发回客户端。请了解更多关于ajax和jquery的一些谷歌搜索...教程你将获得成千上万的。
<script src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
<form>
<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="" placeholder="Profit" id="profit">
<button type="submit" formmethod="POST"> Calculate Profit</button>
</form>
<div id="invoice"></div>
<script>
$('document').ready(function(){
$('button').on('click',function(event){
event.preventDefault();
var units= $("#product_unit").val();
var wholesale_price=$("#wholesale_price").val();
var sell_price=$("#sell_price").val();
var profit = (units*sell_price)-(units*wholesale_price);
$('#profit').val(profit);
var data = $('form').serialize();
$.ajax({
type : "POST",
data : data,
url : "invoice.php",
dataType : "html",
success : function(response){
$('#invoice').html(response);
},
error : function(obj){
console.log(obj);
}
});
});
});
</script>
<强> invoice.php 强>
<?php
$unit = $_POST["units"];
$wholesale = $_POST["wholesale_price"];
$sell = $_POST["sell_price"];
function invoice_profit($units, $wholesale, $sell)
{
return ($units * $sell) - ($units * $wholesale);
}
echo "Invoice #0001";
echo "<table border='1' align='center'>";
echo "<tr>";
echo "<td>" . $_POST["date"] . "</td>";
echo "<td>" . $_POST["product_name"] . "</td>";
echo "<td>" . $_POST["units"] . "</td>";
echo "<td>" . $_POST["wholesale_price"] . "</td>";
echo "<td>" . $_POST["sell_price"] . "</td>";
echo "<td>" . invoice_profit($unit, $wholesale, $sell) . "</td>";
echo "</tr>";
echo "</table>";
?>
结果:
然后你必须根据你的设计设置表格
答案 1 :(得分:0)
您必须首先选择您将在哪里工作。它可以在服务器上或浏览器上。您已经创建了两个函数,这些函数在PHP和JS中返回答案。 PHP中的那个在代码中有一个小错误。你已经把$ unit而不是$ units
$units = $_GET["units"];
$wholesale = $_GET["wholesale_price"];
$sell = $_GET["sell_price"];
function invoice_profit($units, $wholesale, $sell) { // old code: $unit
return ($units * $sell)-($units * $wholesale); // old code: $unit
}
但是这需要提交表格来计算利润。另一种方法是只使用JS。为此,您需要在HTML或像这样的元素中使用id:
echo "<td><input type=\"text\" id=\"the_profit\" value=\"".invoice_profit($units, $wholesale, $sell).."\"></td>";
然后JS会改变这种方式:
function profit_calculation(){
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('the_profit').value = (units*sell_price)-(units*wholesale_price);
}
现在要进行计算,我们需要在某处调用JS函数。如果您希望每次更改都能重新计算利润,那么可以使用每个表单字段完成此类操作:
<input class="form-field" type="date" name="date"
value="" placeholder="Date" id="date"
onchange="return profit_calculation();">
请注意,变量现在是函数的本地变量,并在每次更改时从表单中获取数据。但是在JS和PHP中,你必须检查数据是否也有效。如果你没有JS可以给你NaN或未定义,所以我给出的答案还有一些事情要做。
希望有所帮助,
乙