如何使用javascript和PHP正确创建购物车?

时间:2019-06-25 01:55:41

标签: javascript php jquery ajax

我有一个php代码,用于生成购物车页面,该行的每一行都具有:

  1. 商品价格
  2. 数量
  3. 项目总费用
  4. 整个订单的总费用

前三个我在其中分配了一个类和ID。

例如:

  1. 对于商品的价格,id为“ pr-” +商品的条形码(例如pr-1234)
  2. 数量将为“ inp-” +商品的条形码(例如inp-1234)
  3. 该项目的总费用为“ tl-” +该​​项目的条形码(例如tl-1234)

这是我的PHP代码,可以更好地理解我要传达的内容:

<?php
$cart = '';

$id = $_GET['id'];

$cartsql = $conn->prepare("SELECT Barcode, Quantity FROM tblCart WHERE ContactID = :id", $cursor);
$cartsql->bindValue(':id', $id);
$cartsql->execute();

$cartcount = $cartsql->rowCount();

if($cartcount > 0){
    $cart .= '<section class="section-md bg-default">
                    <div class="container">
                        <div class="table-responsive">
                            <table class="table-cart">
                                <thead>
                                    <tr>
                                        <th colspan="2">Product</th>
                                        <th>Price</th>
                                        <th>Quantity</th>
                                        <th>Total</th>
                                    </tr>
                                </thead>
                                <tbody>';
    while($cartrow = $cartsql->fetch()){
        $productsql = $conn->prepare("SELECT StockDesc, RetCash FROM tblStocks WHERE Barcode = :id", $cursor);
        $productsql->bindValue(':id', $cartrow['Barcode']);
        $productsql->execute();
        $productrow = $productsql->fetch();

        $productimagesql = $conn->prepare("SELECT TOP 1 SliderImage FROM tblSlider WHERE SliderID = :id", $cursor);
        $productimagesql->bindValue(':id', $cartrow['Barcode']);
        $productimagesql->execute();
        $productimagerow = $productimagesql->fetch();

        $total_row = ($cartrow['Quantity'] * $productrow['RetCash']);

        $cart .= '<tr>
                    <td class="table-cart-remove-item">
                        <span class="btn-remove icon icon-sm linear-icon-cross2 icon-gray-4" id="del-'.$cartrow['Barcode'].'"></span>
                    </td>
                    <td style="min-width: 330px">
                        <div class="unit flex-row unit-spacing-md align-items-center">
                            <div class="unit__left">
                                <img src="'.$productimagerow['SliderImage'].'" alt="" width="141" height="188"/>
                            </div>
                            <div class="unit__body">
                                <h6>
                                    <a class="thumbnail-classic-title" href="philippinebrainproducts-detail.php?id='.$cartrow['Barcode'].'">'.$productrow['StockDesc'].'</a>
                                </h6>
                            </div>
                        </div>
                    </td>
                    <td>
                        <div id="pr-'.$cartrow['Barcode'].'" class="product-price"><span>'.number_format($productrow['RetCash'],2).'</span></div>
                    </td>
                    <td>
                        <input class="inp-quantity form-input" type="number" id="inp-'.$cartrow['Barcode'].'" data-zeros="true" value="'.$cartrow['Quantity'].'" min="1">
                    </td>
                    <td>
                        <div id="tl-'.$cartrow['Barcode'].'" class="product-price"><span>'.number_format($total_row,2).'</span></div>
                    </td>';
    }


    $cart .= '                 </tbody>
                             </table>
                        </div>
                    <div class="row row-50 text-center">
                        <div class="col-sm-12">
                            <div class="text-md-right">
                                <dl id="subtotal" class="list-terms-minimal">
                                    <dt>Subtotal</dt>
                                    <dd></dd>
                                </dl>
                                <dl id="total-price" class="heading-5 list-terms-minimal">
                                    <dt>Total</dt>
                                    <dd>//total cost will be displayed here</dd>
                                </dl>
                                <a class="button button-primary" href="checkout.php"> Proceed to Checkout</a>
                            </div>
                        </div>
                    </div>
                </div>
          </section>';
}

echo $cart;
?>

我的问题是,更改数量后,如何更改物料的总成本和订单的总成本?我正在使用输入类型编号作为数量。

到目前为止,我要做的是获取输入类型数字的类以获取值。如何正确获取数量并根据数量更改物料和订单的成本?

$(document).ready(function() { 
    $(document).on("change",".inp-quantity",function() {
        var quantity = $(this).val();
        var id_name = $(this).attr('id');
        var split = id_name.split("-");
        var id = split[1];

        //HERE IS WHERE I SAVE THE QUANTITY OF THE ORDER
        $.ajax({
            url: "change-single-item-total.php",
            method: "POST",
            data: {quantity: quantity,id: id},
            success: function(data){

            },
            error: function(data){
                console.log(data);
            }
        });
    });
});

0 个答案:

没有答案