gpt4 book ai didi

javascript - 如何使用 javascript/jquery 从 php 获取总计

转载 作者:行者123 更新时间:2023-11-29 02:44:00 25 4
gpt4 key购买 nike

现在是第 3 天,我正在尝试弄清楚如何在数量变化时更新总计。我已经为价格和总计做了更新,因为我改变了数量,但总计我无法做到。这是我的代码

  <?php 
if(isset($_POST['prod_id'])){
$prod_id =$_POST['prod_id'];
$prod_qty=$_POST['qty'];
$up_cart="UPDATE `cart` SET `qty` = '$prod_qty' WHERE `cart`.`product_id` = '$prod_id'";
$up_cart_run = mysqli_query($conn,$up_cart);
if($up_cart_run){
echo "done";
}

}
?>


<?php


$cart_item_query="SELECT * FROM cart";
$cart_item_run = mysqli_query($conn,$cart_item_query);
if (mysqli_num_rows($cart_item_run)>0){
$grand_total=0;
while($cart_row=mysqli_fetch_array($cart_item_run)){
$pro_id = $cart_row['product_id'];
$pro_qty = $cart_row['qty'];


$product_query="SELECT * FROM products WHERE product_id = '$pro_id'";
$product_run = mysqli_query($conn, $product_query);
$product_row = mysqli_fetch_array($product_run);
$pro_name = $product_row['product_name'];
$pro_image = $product_row['product_image'];
$pro_sell_price = $product_row['sell_price'];
$total = $pro_sell_price*$pro_qty;
$grand_total =$grand_total+ $total;


?>

<div class="row">

<div class="col-md-3 col-xs-3"><img src="img/<?php echo $pro_image; ?>" alt="" width="50px"></div>
<div class="col-md-3 col-xs-3"><p><?php echo $pro_name; ?></p><a href=""><p><i class="fa fa-trash" aria-hidden="true"></i> Remove</p></a></div>
<div class="col-md-2 col-xs-2">
<select class="change_qty" size="1" style="width:50px;">
<?php
for ($i = 1; $i <= 9; $i++) {
$selected = $pro_qty == $i? 'selected' : '';
echo "<option class='qty' data-pro='{$pro_id}' value='{$i}' {$selected}>{$i}</option>";
}
?>
</select>
</div>

<div class="col-md-2 col-xs-2"><input data-pro="<?php echo $pro_id; ?>" id="price-<?php echo $pro_id; ?>" type="text" value="<?php echo $pro_sell_price; ?>" disabled class="form-control dis-input price"></div>
<div class="col-md-2 col-xs-2"><input id="total-<?php echo $pro_id; ?>" data-pro="<?php echo $pro_id; ?>" type="text" value="<?php echo $total; ?>" disabled class="form-control dis-input total"></div>

</div>

<hr>

<?php

}
}

?>




</div>
</div>
</div>
<div class="col-md-3">
<div class="card">
<div class="card-body">
<div id="cart_total"></div>

<h3>TOTAL <span class="pull-right" id="grand_total">RS. <?php echo $grand_total;?></span></h3> <br>
<a href="" class="btn btn-secondary btn-block">PLACE ORDER</a><br>
<h5>ORDER SUMMARY</h5>
<P>Total Price <span class="pull-right">RS.50000</span> </P>
<P>Discount <span class="pull-right text-success">- Rs.0</span> </P>
<P>Sub Total <span class="pull-right">Rs.60000</span> </P>
<P>Estimated GST <span class="pull-right">Rs.0</span> </P>
<P>Delivery Charges <span class="pull-right">Rs.0</span> </P><hr>
<h5>Total Payable <span class="pull-right">RS. 60000</span> </h5>


</div>
</div>

这里是js

$('body').delegate('.change_qty','change',function(){
var qty = $(this).val();
var prod_id = $('option:selected', this).attr('data-pro');
var price = $('#price-'+prod_id).val();
var total = qty*price;
$('#total-'+prod_id).val(total);


up_cart();
function up_cart(){
$.ajax({
url:'cart.php',
method:'POST',
data:{prod_id:prod_id,
qty:qty
},
success:function(response){

}
});
}
});

现在除了总计之外一切正常。页面刷新总计更新,但我希望它随着价格等数量的变化而更新。谢谢。

最佳答案

您必须创建一个 PHP 文件,例如“calc_grand_total.php”之类的文件,并在您的 ajax 调用中使用它代替“cart.php”。在您的 PHP 文件中,您应该使用发送到文件的参数计算总计,然后返回文章的总计。在 ajax 调用的成功函数中,您可以使用 jQuery 在 cart.php 中输出和刷新总计。

关于javascript - 如何使用 javascript/jquery 从 php 获取总计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45878683/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com