【发布时间】:2021-05-07 07:01:15
【问题描述】:
我正在做帐单,当我添加多个产品时,所有产品的价格都是一样的。 正因为如此,我什至无法得到所有这些的总和但我做不到,但我正在为此苦苦挣扎。请检查我的代码并告诉我答案。
bill.php
<?php
include 'head.php';
?>
<div>
<table class="table table-sm table-bordered table-striped">
<thead>
<tr>
<th>Product Name</th>
<th>Quantity</th>
<th>Price</th>
<th>Total Amount</th>
<th><input class="addrow" type="button" name="" value="Add"></th>
</tr>
</thead>
<tbody>
<tr>
<td><select class="form-control">
<option value="">--select--</option>
</select></td>
<td style="width: 100px"><input class="form-control quantity" type="number" name=""></td>
<td style="width: 100px"><input class="form-control price" value="" disabled type="text" name=""></td>
<td style="width: 200px"><input class="form-control amount" value="" disabled type="text" name=""></td>
<td><input class="deleterow" type="button" name="" value="Delete"></td>
</tr>
</tbody>
</table>
</div>
<script>
$(document).ready(function(){
$.ajax({
url:"webservice.php",
type:"post",
dataType:'json',
success:function(data){
$('.table tbody select').append(data);
},
error:function(){
console.log("ERROR IN AJAX");
}
});
});
$(document).on('change','select',function(){
let product_value = $(this).val();
$.ajax({
url:"productprice.php",
type:"post",
data:{product_value},
dataType:'json',
success:function(data){
$('.price').val(data);
},
error:function(){
console.log("ERROR IN AJAX");
}
});
});
$(document).on('click','.addrow',function(){
$('.table tbody tr:first-child').clone().appendTo('.table tbody');
});
$(document).on('click','.deleterow',function(){
if($('.table tbody').children().length == 1){
alert("minimum 1 Product required");
}else{
$(this).closest('tr').remove();
}
});
$(document).on('change','.quantity',function(){
let quantity = $('.quantity').val();
let price = $('.price').val();
$('.amount').val(quantity * price);
});
</script>
webservices.php
<?php
include 'connection.php';
$sql = "SELECT * FROM `products`";
$result = mysqli_query($db,$sql);
// $output ="";
while ($row = $result->fetch_assoc()) {
$output[] = "<option value=$row[product_id]>$row[product_name]</option>";
}
echo json_encode($output);
productprice.php
<?php
include 'connection.php';
$sql = "SELECT * FROM `products`";
$result = mysqli_query($db,$sql);
// $output ="";
while ($row = $result->fetch_assoc()) {
$output[] = "<option value=$row[product_id]>$row[product_name]</option>";
}
echo json_encode($output);
【问题讨论】: