使用 PHP 进行 OnChange Caluclations 的计算

OnChange Caluclations With PHP

本文关键字:计算 Caluclations OnChange PHP 进行 使用      更新时间:2023-09-26

>我有一个订单,一旦在文本框中输入数量,我希望税和总计适当更新。 如何使用php完成此操作,或者这是一个需要调用的javascript函数? 举个例子,假设税收将是0.3%

我所追求的:
1) txtitem1qty 的 OnChange() 填充税 (txtitem1tax)
2) txtitem1qty 的 OnChange() 填充总计 (txtitemtotalprice)

这是我到目前为止填充我的订单表的代码

<html>
<body>
    <form id="Form1" runat="server">
        <div id="Form1" runat="server">
        <table id="table1" border="1">
            <tr>
                <th>Item</th>
                <th>Price</th>
                <th>Quantity</th>
                <th>Tax</th>
                <th>Total</th>
            </tr>
            <tr>
                <td><label for="item1">Item 1</label></td>
                <td><label for="lblitem1price">$25.00</label></td>
                <td><input  type="text" name="txtitem1qty" value="0" maxlength="10" size="3"></td>
                <td><input  type ="text" name="txtitem1tax" maxlength="10" size="3" readonly></td>
                <td><input  type="text" name="txtitem1totalprice" maxlength="10" size="3" readonly></td>
            </tr>
        </table>
        </div>
    </form>
</body>
</html>

我为您做了一个解决方案示例:

我在您的 html 中做了一些必要的更改,包括"ids":

文件/

/html 文件

<tr>
                <td><label for="item1">Item 1</label></td>
                <td><label for="lblitem1price">$25.00</label></td>
                <td><input  type="text" id="item_1_qty" name="txtitem1qty" value="0" maxlength="10" size="3"></td>
                <td><input  type ="text" id="item_1_tax" name="txtitem1tax" maxlength="10" size="3" readonly></td>
                <td><input  type="text" id="item_1_totalprice" name="txtitem1totalprice" maxlength="10" size="3" readonly></td>
            </tr>

HTML 文件中的 JavaScript

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-beta1/jquery.min.js"></script>
<script>
    function calculate(el){
        var quantity = el.val();
        var id = el.attr("id").replace("item_","").replace("_qty","");
        var data = {quantity: quantity,id:id};
        var targetTax = $("#item_"+id+"_tax");
        var targetTotalPrice = $("#item_"+id+"_totalprice");
        $.post("sandbox.php",data,function(response){
           response = JSON.parse(response);
            targetTax.val(response.tax);
            targetTotalPrice.val(response.totalprice);
        });
    }
    var qty = $("#item_1_qty");
    qty.on("keyup",function(){
        calculate($(this));
    });
</script>

我的文件称为沙盒,但可以重命名为计算.php;

沙盒.php

<?php
//The best way is that the data is in a database
$items = array(
    array("id"=> 1, "label"=> "Item1","value"=>25.0)
);
$tax = 0.3;
$id = isset($_POST['id'])?$_POST['id']:0;
if(!is_numeric($id)){
    $id = 0;
}
$quantity = isset($_POST['quantity'])?$_POST['quantity']:1;
if(!is_numeric($quantity)){
    $quantity = 1;
}
$finalValue = 0;
$currentItem = null;
foreach($items as $item){
    if($item["id"] == $id){
          $currentItem = $item;
    }
}
if($currentItem != null){
    $finalValue = $quantity * $currentItem["value"];
    $finalValue = ($finalValue * $tax) + $finalValue;
}
$return = array(
    'quantity' => $quantity,
    'tax' => $tax,
    'totalprice' => $finalValue
);
print json_encode($return);

更新:在数据库中查找产品只是您可以应用的示例:

$db = new mysqli('localhost', 'user', 'pass', 'demo');
if($db->connect_errno > 0){
    die('Unable to connect to database [' . $db->connect_error . ']');
}
$sql = "SELECT p.id,p.value FROM Products p WHERE p.id = ?";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param('i', $id);
/* execute prepared statement */
$stmt->execute();
/* close statement and connection */
$stmt->close();
if(!$result = $stmt->get_result()){
    die('There was an error running the query [' . $db->error . ']');
}
//$items = $result->fetch_all(MYSQLI_ASSOC);
$currentItem = $result->fetch_array();
$tax = 0.3;
$id = isset($_POST['id'])?$_POST['id']:0;
if(!is_numeric($id)){
    $id = 0;
}
$quantity = isset($_POST['quantity'])?$_POST['quantity']:1;
if(!is_numeric($quantity)){
    $quantity = 1;
}
$finalValue = 0;

if($currentItem != null){
    $finalValue = $quantity * $currentItem["value"];
    $finalValue = ($finalValue * $tax) + $finalValue;
}
$return = array(
    'quantity' => $quantity,
    'tax' => $tax,
    'totalprice' => $finalValue
);
print json_encode($return);