PHP - 更新购物车总金额而不刷新页面

时间:2015-01-09 06:03:16

标签: php jquery ajax

底部排队前面我正在尝试更新结帐页面中的购物车总金额,确切地说,是在存储所有导航链接的标题区域以及确认顺序中手风琴。为了做到这一点,我必须刷新页面。如何在没有页面刷新的情况下尝试实现它?

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

一旦完成,下一个手风琴就会要求确认购物车在哪里可以看到购物车的完整细节。在那里,我想显示更新的购物车金额,即用户必须支付的the cart total amount + the shipping zone amount

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

问题:如何在点击运送区域中的按钮时更新会话值,并将其显示给用户,而无需双面刷新页面 - 在标题区域和confimation手风琴中?任何帮助将受到高度赞赏。谢谢。

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

<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";
}

1 个答案:

答案 0 :(得分:0)

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

成功执行ajax请求后。然后以json格式返回成功消息,total amt,shipping zone等。

然后解析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.
});