计算sum,multiplication并在输入值时自动生成结果

时间:2014-09-17 13:02:34

标签: javascript php

我在php表单中有一些计算。

实际上,只要我在其他文本框中输入值,我就必须在总计(文本框)中显示一些计算。

我做到了。但如果添加了多行(动态添加),那么该代码对第二行或第三行不起作用......

我不知道该怎么做。这是我的完整表格。

                       <div class="w-box">
                            <div class="w-box-header">
                                <h4>Add order line items</h4>
                            </div>
                            <div class="w-box-content">
<form name="users" method="post" action="orders_line_items.php" enctype="multipart/form-data" onSubmit="return validate();" id="inv_form">
 <div class="formSep">

<?php
$sql2 = "SELECT * FROM item_category";
$query2 = mysqli->query($sql2);
$row = $result->fetch_array(MYSQLI_ASSOC);
?>

<select name="category" onChange="showItems(this);">
  <option value="">Item Category</option>
<?php while ($rs2 = $query2->fetch_array(MYSQLI_ASSOC)) { ?>
  <option value="<?php echo $rs2["item_id"]; ?>"><?php echo $rs2["name"]?></option>
<?php } ?>
</select>

<input type="hidden" name="order_id" value="<?php echo $_SESSION['order_id']; ?>" />
&nbsp;&nbsp;&nbsp; 

 <div id="output1"></div> 

 <div id="output2"></div>

 &nbsp; &nbsp; &nbsp; <input type="submit" name="add" id="save" value="ADD" class="btn btn-info" />

 </div>

</form> 
<?php

if(isset($_POST['add']))
{ 
$order_id = $_POST['order_id'];
$category = $_POST['category'];
$items = $_POST['items'];
$item= $_POST['item'];
$descr = $_POST['description'];
$uom = $_POST['uom'];
$cost = $_POST['cost'];
$selling_price = $_POST['selling_price'];
$tax_rate = $_POST['tax_rate']; 




$sql = mysqli->query("insert into temp_order_line_items (id, order_id, item, description, uom, tax, selling_price, discount, notes, delivery_date, freight, freight_tax, total) values ('', ".$order_id.", '".$item."', '".$descr."', '".$uom."', ".$tax_rate.", ".$selling_price.", '', '', '', '', '', '')");

if($sql)
{
          ?>
           <div class="w-box">
                            <div class="w-box-header">
                                <h4>Line Items</h4>
                            </div>
                            <div class="w-box-content">
                            <div class="formSep">
<form name="department" method="post" action="" enctype="multipart/form-data" onsubmit="return validate();" id="inv_form">

          <?php    
                   $m1 = "select * from temp_order_line_items where order_id = '".$_SESSION['order_id']."'";
                   $query1 = mysqli->query($m1);


while ($row1=$query1_fetch_assoc())
{
 $tax = $row1['tax'];
if($tax == 5)
{
    $tax_name = 'VAT';
     }
else if($tax == 2)
{
    $tax_name = 'CST';
     }
else if($tax == 12.36)
{
    $tax_name = 'Service';
     }  


    ?>


  <input type="text" class="span2"  name="item[]" id="category" value="<?php echo $row1['item'];?>"  />&nbsp;&nbsp;
  <input type="text" class="span1" name="uom[]" id="category" value="<?php echo $row1['uom'];?>"  />&nbsp;&nbsp;
  <input type="text" name="description[]" id="category" value="<?php echo $row1['description'];?>"  />&nbsp;&nbsp;

 <input type="text" class="span1" name="quantity[]" id="qty"  onkeyup="getValues(1)" />&nbsp;&nbsp;

 <input type="text" class="span1" name="price[]" id="price" value="<?php echo $row1['selling_price'];?>" onkeyup="getValues(2)" />&nbsp;&nbsp;

 <input type="text" class="span1" name="discount[]" id="discount" value="<?php echo $row1['discount'];?>" onkeyup="getValues(3)" />&nbsp;&nbsp;
 <select name="tax[]" class="span1" id="tax" onkeyup="getValues(4)">
 <option value="<?php echo $row1['tax'];?>"><?php echo $row1['tax'];?></option>
 <?php $l1 = mysql_query("select * from taxes") or die (mysql_error());
 while($l2 = mysql_fetch_array($l1))
 { ?>
 <option value="<?php echo $l2['rate']; ?>"> <?php echo $l2['name'];?> - <?php echo $l2['rate'];?>% </option>
 <?php } ?> </select>&nbsp;&nbsp;



<input type="text" class="span1" name="freight[]" id="freight" onkeyup="getValues(5)" placeholder="Freight"  />&nbsp;&nbsp;

<input type="text" class="span2" name="total[]" id="total" />&nbsp;&nbsp;

 <input type="hidden" name="del_date" id="category" value="<?php echo $row1['delivery_date'];?>"  />
 <input type="hidden" name="notes" id="category" value="<?php echo $row1['notes'];?>"  />





<?php                     

}
echo '<input type="submit" name="save" id="save" value="UPDATE" class="btn btn-info" />


</form> </div>
</div>';
}
}
 ?>
</div>
</div>

我的javascript计算总数

<script language="javascript">
function getValues(val){

var numVal1=parseInt(document.getElementById("qty").value);
var numVal2=parseInt(document.getElementById("price").value);
var numVal3=parseInt(document.getElementById("discount").value);
var numVal4=parseFloat(document.getElementById("tax").value);

var numVal5=parseFloat(document.getElementById("freight").value);


var sub_total = (numVal1 * numVal2) - numVal3;
var subt_tax =  sub_total * (numVal4/100);
var total = parseFloat(sub_total + subt_tax);
var totalValue = parseFloat(total + numVal5);

document.getElementById("total").value = totalValue;
}
</script>

这可以这样做,

选择类别,然后选择项目,然后项目详细信息将从文本框中的数据库自动填充。当我点击添加它将被添加到一个表并显示在同一页面。我在这里做计算。如果我添加单个项目,那么它可以正常工作。如果我添加超过1,它不起作用。

请建议

0 个答案:

没有答案