简体   繁体   English

Javascript从mysql计算小计和总计

[英]Javascript calculate subtotal and total from mysql

First of all, I'm sorry if this question has already been asked and solved before, but I cant find the exact answer/solution for my problem. 首先,对于此问题之前是否曾被提出和解决过,我感到抱歉,但是我找不到我的问题的确切答案/解决方案。

My question is, how do I calculate grand-total if a user changes quantity and/or selects more that 1 product? 我的问题是,如果用户更改数量和/或选择多于一种产品,如何计算总计? My product item list and value is from mysql. 我的产品项目列表和值来自mysql。

Here's my form details: 这是我的表格详细信息:

<?php $price = 10.00;?>
<input name="qty_<?php echo $obj->product_code;?>" type="text" id="qty<?php echo $obj->product_code;?>" value="0" size="2" onkeyup="calculate()">

<input name="subtotal_<?php echo $obj->product_code;?>" type="text" id="subtotal_<?php echo $obj->product_code;?>" value="0.00" size="7" readonly>

<input name="qty_<?php echo $obj->product_code;?>" type="text" id="qty<?php echo $obj->product_code;?>" value="0" size="2" onkeyup="calculate()">

<input name="subtotal_<?php echo $obj->product_code;?>" type="text" id="subtotal_<?php echo $obj->product_code;?>" value="0.00" size="7" readonly>

<input name="grandtotal" type="text" id="grandtotal" value="0.00" readonly>

I'm using my php/mysql results value for my input field name/id, as it's easier for me to identify/pass the value when submitted. 我正在使用我的php / mysql结果值作为我的输入字段名称/ id,因为提交时我更容易识别/传递该值。

Here's my Javascript: 这是我的Javascript:

function calculate()
{
    var quantity = document.getElementById('<?php echo $obj->product_code;?>').value;
    var currprice = <?php echo $obj->product_code.$obj->product_price;?>;
    var total = quantity * currprice;
    document.getElementById('subtotal_<?php echo $obj->product_code;?>').value = total.formatMoney(2,',','.');
}

So, how do I calculate the subtotal from each product and display it at grand total text field? 那么,如何计算每个产品的小计并将其显示在总计文本字段中? I've already searched Google but have not been able to solve my problem. 我已经搜索过Google,但无法解决我的问题。

1) Using the right ID's 1) 使用正确的ID

The id's of the quantity fields do not match with the getElementById in the function. 数量字段的ID与函数中的getElementById不匹配。 It should start with "qty_", not directly the product code. 它应以“ qty_”开头,而不是直接以产品代码开头。 Also, for the sake of regularity, change the id's in the <input> 's from "qty" to "qty_". 另外,出于规律性考虑,将<input>的id从“ qty”更改为“ qty_”。

2) The right structure 2) 正确的结构

The html you use now, has double quantity and subtotal <input> fields with the same ID. 您现在使用的html具有相同ID的双数量和小计<input>字段。 This causes Javascript to be unable to access these values. 这导致Javascript无法访问这些值。 Make sure you distinguish between the names and ID's of these input fields. 确保您区分这些输入字段的名称和ID。

PHP : PHP的

 $products = array( array("price"=>10, "code"=>"product1"), array("price"=>25, "code"=>"product2") ); foreach($products as $key => $productInfo) { /* echo a hidden field with the price per product (for calculation later) */ echo '<input type="hidden" name="price_'. $productInfo["code"] .'" id="price_'. $productInfo["code"] .'" value="'. $productInfo["price"] .'" />'; /* echo the quantity field for this product */ echo '<input class="qty" name="'. $productInfo["code"] .'" id="'. $productInfo["code"] .'" type="text" value="0" size="2" onkeyup="calculate()">'; /* echo the subtotal field for this product */ echo '<input name="sub_'. $productInfo["code"] .'" type="text" id="sub_'. $productInfo["code"] .'" value="0.00" size="7" readonly>'; } /* echo the field for total price */ echo '<input type="text" name="total" id="total" value="0.00" />'; 
This results in sets of three input fields per product: 这将导致每个产品三个输入字段的集合:

  • One for the price per product (id="price_THEPRODUCTCODE") 一个代表每个产品的价格(id =“ price_THEPRODUCTCODE”)
  • One for the quantity (id="THEPRODUCTCODE") 一个代表数量(id =“ THEPRODUCTCODE”)
  • And one for the subtotal (id="sub_THEPRODUCTCODE") 还有一个用于小计(id =“ sub_THEPRODUCTCODE”)

Javascript Java脚本

I suggest using the jQuery library for faster and easier collection of the data from the input fields here. 我建议使用jQuery库从这里的输入字段中更快,更轻松地收集数据。

The function would then be: 该函数将是:

 function calculate() { var total = 0; // for each quantity field: $("input.qty").each(function() { // get product code var productCode = $(this).attr("id"); // get price per product var price = $("#price_"+productCode).val(); // get quantity var quantity = $(this).val(); // calculate subtotal var subtotal = (price*quantity); // put new subtotal back in the input field $("#sub_"+productCode).val(subtotal); // add subtotal to total total += subtotal; }); // put total in the input field of total $("#total").val(total); } 

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

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