PHP - 在不刷新页面的情况下更新购物车总额


PHP - Updating cart total amount without page refresh

底线在前面: 我正在尝试更新结帐页面中的购物车总额,准确地说,在存储所有导航链接的标题区域以及确认订单手风琴中。为此,我必须刷新页面。如何在不刷新页面的情况下尝试实现它?

详细信息:当用户进入结帐页面时,他必须填写运输表格才能应用运费。这通常是一个选择框,他将根据他的送货地址选择区域。选择区域并单击按钮后,我调用 $.ajax 方法插入到数据库表中,并且它确实被正确插入。

完成后,下一个手风琴出现,询问购物车的确认,他将在哪里看到购物车的完整细节.在那里,我想显示更新的购物车金额,意思是用户必须支付的the cart total amount + the shipping zone amount

我可以成功获取更新的购物车总额,但无法向用户显示。用户每次都必须刷新页面才能看到更新的购物车总额。这反过来又导致程序再次从选择不应该发生的运输区域开始。

问题:如何在单击运输区域中的按钮时更新会话值并将其显示给用户,而无需在两侧刷新页面 - 在标题区域和确认手风琴中?任何帮助将不胜感激。谢谢。

这是我选择区域的选择框:

<select name="selectZone" id="custDelAddZone">
    <option value="-1">----- Select -----</option>
    <?php
    $queryForZone = "SELECT * FROM shipZones";
    $validate->Query($queryForZone);
    if ($validate->NumRows() >= 1) {
        while ($row = $validate->FetchAllDatas()) {
            echo '<option value="'.$row['Id'].'">'.$row['Name'].'</option>';
        }
    }
    ?>
</select>

这是我的结帐页面(确认手风琴):

<?php
if ( isset( $_SESSION['cart'] ) && $_SESSION['cart'] != "" ) {
    $total = 0;
    $subTotal = 0; $sbTotal = 0;
    $taxAmount = $tax = $totalTaxAmount = $taxAmt = 0;
    $cartWeightPerProduct = $totalCartWeight = $amtWeight = 0;
    $sql = "SELECT p.*, c.*, ws.* FROM products p, categories c, weight_shipping ws WHERE ProdCode IN (";
    foreach ( $_SESSION['cart'] as $id => $value ) {
        $sql .= '"'.$id.'",';
    }
    $sql = substr( $sql, 0, -1 ) . ") AND p.CatId = c.CatId AND ws.ProdId = p.ProdId";
    if ($validate->Query($sql) == TRUE) {
        if ($validate->NumRows() >= 1) {
            while ( $row = $validate->FetchAllDatas() ) {
                echo '<tr>';
                echo '<td><img src="images/Products/'.$row['ProdCode'].'.jpg" alt="'.$row['ProdCode'].'"><a href="product.php?code='.$row['ProdCode'].'">'.$row['ProdName'].'</a></td>';
                echo '<td>'.$row['ProdCode'].'</td>';
                echo '<td>Rs. '.$row['ProdRate'].'</td>';
                echo '<td>'.$_SESSION['cart'][$row['ProdCode']]['quantity'].'</td>';
                $sbTotal = $row['ProdRate'] * $_SESSION['cart'][$row['ProdCode']]['quantity'];
                $subTotal = $sbTotal;
                echo '<td>'.number_format($sbTotal,2).'</td>';
                $total += $subTotal;
                $_SESSION['cartTotalAmount'] = $total;
                $tax = $row['CatTaxPercent'];
                $taxAmt = (($sbTotal * $tax ) / 100);
                $taxAmount += $taxAmt;
                $amt = 0;
                $cartWeightPerProduct = ($row['weight'] * $_SESSION['cart'][$row['ProdCode']]['quantity']);
                echo '</tr>';
                $totalCartWeight += $cartWeightPerProduct;
            }
            $totalTaxAmount += $taxAmount;
            $_SESSION['cartWeight'] = $totalCartWeight;
            $sessAmnt = ($total + $totalTaxAmount);
            $totalPayableAmnt = $sessAmnt + $_SESSION['TotalWeight'];
            $_SESSION['sessionTotalPayable'] = number_format($totalPayableAmnt, 2);
            if ( isset( $_SESSION['sessionTotalPayable'] ) ) {
                $amt = $totalPayableAmnt;
            } else {
                $amt = "Rs. 0";
            }
            echo '<tr><td>Cart Total:</td><td>'.number_format($total,2).'</td></tr>';
            echo '<tr><td>Taxes:</td><td>'.number_format($totalTaxAmount,2).'</td></tr>';
            echo '<tr><td>Shipping:</td><td>'.number_format($_SESSION['TotalWeight'],2).'</td></tr>';
            echo '<tr><td>Total Payable Amount:</td><td>'.number_format($amt,2).'</td></tr>';
        }
    }
}

这是我用来插入的 AJAX 代码:

$.ajax({
    var dataStr = $("#chooseShippingZoneForm").serialize();
    url: 'http://localhost/ECommerce/ActionFiles/Customers/UpdateDeliveryZone.php',
    type: 'POST',
    data: dataStr,
    success: function(msg) {
        toastr.success(msg);
        toastr.options.showMethod = "slideDown";
        toastr.options.hideMethod = "slideUp";
    }
});

这是UpdateDeliveryZone.php

<?php
session_start();
$zoneId = $custCode = "";
require_once '../../Classes/class.Validation.php';
$validate = new Validation('developi_ecommerce');
$q = "SELECT CustCode FROM customers WHERE CustEmailAdd = '".$_SESSION['Customer']['email']."'";
$validate->Query($q);
if ($validate->NumRows() >= 1) {
    while ($row = $validate->FetchAllDatas()) {
        $custCode = $row['CustCode'];
    }
} else {
    echo "No Customer Found";
}
if ( isset( $_POST['selectZone'] ) && $_POST['selectZone'] != "" ) {
    $zoneId = $validate->EscapeString( $_POST['selectZone'] );
    $query = "UPDATE customers_delivery_address SET ZoneId = '".$zoneId."' WHERE CustCode = '".$custCode."' AND CustDelAddLastInserted >= NOW() - INTERVAL 10 MINUTE";
    if ( $validate->Query( $query ) == TRUE ) {
        echo "Updated Successfully";
    } else {
        echo "Invalid Query";
    }
} else {
    echo "Value Not Set";
}

选择区域后,您将调用 $.ajax 将数据发布到数据库中。

一旦 ajax 请求成功执行。然后以 json 格式返回成功消息、总 amt、运输区域等。

然后解析 JSON 并进行计算,并将值放在适当的元素中。

以同样的方式,您可以使用 sessionstorage() 方法更新客户端上的会话。

示例 jQuery 代码

$.ajax({
  type: "POST",
  url: "update.php",
  data: { **data to be passed** }
})
.done(function( resp ) {
    // Parse the response and place the values in appropriate sections
    $("#confirmdiv #totalPrice").html(resp.totalPrice);
    // Other sections that needs to be updated.
});