简体   繁体   English

将html表单元素作为参数传递给php函数

[英]pass html form elements as arguments to a php function

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. 但无法理解如何将输入准确传递给php函数,然后将其写回到表单元素。 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, 和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. 因此,基本上,单位,批发价格和卖方价格将传递给php函数,利润将被计算并写回各自的表单ID。 Please help. 请帮忙。

You will need ajax to send back the invoice from the server to the client. 您将需要ajax将发票从服务器发送回客户端。 Please learn more about ajax and jquery a little google search.. of tutorials you will get thousands of them. 请详细了解有关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 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>";
?>

Results : 结果:

在此处输入图片说明

Then you will have to style the table according to your design 然后,您将不得不根据您的设计来设计桌子的样式

You must first choose where you will do the job. 您必须首先选择要在哪里工作。 It can be on the server or on the browser. 它可以在服务器或浏览器上。 You have created two functions that return the answer in PHP and in JS. 您已经创建了两个函数,这些函数在PHP和JS中返回答案。 The one in PHP has a small bug in the code. PHP中的一个在代码中有一个小错误。 You have put $unit instead of $units 您放置了$ 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    
}

But this will need a submit the form to calculate the profit. 但这需要提交表格以计算利润。 Another way is to use only JS. 另一种方法是仅使用JS。 For this you will need and id in the HTML or an element like this: 为此,您将需要HTML中的id和以下元素:

echo "<td><input type=\"text\" id=\"the_profit\" value=\"".invoice_profit($units, $wholesale, $sell).."\"></td>";

Then the JS will change this way: 然后,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);     
}

Now to do the calculation we need to invoke the JS function somewhere. 现在进行计算,我们需要在某个地方调用JS函数。 If you want every change to recalculate profit then something like this can be done with every form field : 如果您希望每次更改都可以重新计算利润,则可以对每个表单字段执行以下操作

   <input class="form-field" type="date" name="date"
          value="" placeholder="Date" id="date"
          onchange="return profit_calculation();">

Note that the variables are now local to the function and take the data from the form on every change. 注意,变量现在是函数的局部变量,每次更改都会从表单中获取数据。 But in JS and in PHP you have to check if the data is valid also. 但是在JS和PHP中,您还必须检查数据是否也有效。 If you don't JS can give you NaN or undefined, so the answer I give has still some things more to be done. 如果您不这样做,JS可以给您NaN或undefined,因此我给出的答案还有很多事情要做。

Hope that helps, 希望能有所帮助,

B. B.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM