使用 PHP 进行 OnChange Caluclations 的计算
OnChange Caluclations With PHP
>我有一个订单,一旦在文本框中输入数量,我希望税和总计适当更新。 如何使用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);
相关文章:
- 根据元素和容器大小计算边距
- 从Rally获取一个特定的标记,以便计算另一个字段中的值
- 使用D3.js计算带有字母间距的文本长度
- 使用CSS或JavaScript计算分页符的数量
- 可以't计算自定义谷歌地图的js
- 如何计算每个元素's的高度,并将这些值用作函数中的变量
- JavaScript计算帮助(乘以时间)
- 如何计算对象文字中的键
- JavaScript循环无法正确计算/显示结果
- 与域在同一台计算机上运行的NODEJS服务器的CORS错误
- 四舍五入JavaScript计算
- 计算HTML中的页数
- 使用jQuery计算数组中的对象以更改进度条
- 如何在jquery中使用实时计算求和值
- 计算多个项目的价格
- 计算CSS3缩放框在另一个框中的最高位置
- 如何计算二十面体的法线
- if(foo!==null)的计算结果为true,即使foo为null
- 在Angular中重新使用HTML端的计算文本
- 使用 PHP 进行 OnChange Caluclations 的计算