[英]pass html form elements as arguments to a php function
我正在尝试计算利润,当用户输入其他详细信息时将动态显示利润。 但无法理解如何将输入准确传递给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>"
?>
因此,基本上,单位,批发价格和卖方价格将传递给php函数,利润将被计算并写回各自的表单ID。 请帮忙。
您将需要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>";
?>
结果:
然后,您将不得不根据您的设计来设计桌子的样式
您必须首先选择要在哪里工作。 它可以在服务器或浏览器上。 您已经创建了两个函数,这些函数在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或undefined,因此我给出的答案还有很多事情要做。
希望能有所帮助,
B.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.