PHP-更新购物车总额而无需刷新页面 [英] PHP - Updating cart total amount without page refresh

查看:39
本文介绍了PHP-更新购物车总额而无需刷新页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最前面的底线:我正在尝试在结帐页面上(更确切地说)在存储所有导航链接的标题区域并按确认顺序更新购物车总金额手风琴.为此,我必须刷新页面.我如何尝试在不刷新页面的情况下实现它?

Bottom Line Up Front: I am trying to update the cart total amount in the checkout page, to be precise, on the header area where all the navigation links are stored and also in the confirm order accordion. In order to do that I have to refresh the page. How can I try to achieve it without page refresh ?

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

Detailed Information: When the user comes to the checkout page, he has to fill out the shipping form in order to apply the shipping rate. This is normally a select box where he will choose the region depending upon his shipping address. After selecting the region and clicking on the button, I am calling the $.ajax method to insert into the database table and it does get inserted correctly.

完成后,下一个手风琴出现,询问购物车的确认信息,他将在其中看到购物车的完整详细信息.在那儿,我想显示更新的购物车金额,即用户必须支付的购物车总金额+运送区域的金额.

Once it is done, the next accordion comes up asking the confirmation of the cart where in he will see the complete details of the cart. Over there I want to show the updated cart amount, meaning, the cart total amount + the shipping zone amount which the user has to pay.

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

I can successfully get the updated cart total amount but cannot show it to the user. The user has to refresh the page every time in order to see the updated cart total amount. This in turn leads to the procedure once again starting from choosing the shipping zone which should not happen.

问题:如何单击运输区域中的按钮来更新会话值,并在不刷新页面的情况下将其显示给用户-在页眉区域以及在确认手风琴中?任何帮助将不胜感激.谢谢.

Question: How do I update the session value on click of the button which is in the shipping zone and display it to the user without page refresh on both side - In the header area and also in the confimation accordion ? Any help would be highly appreciated. Thank You.

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

Here's my select box to choose the zone:

<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>

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

Here's my checkout page (Confirmation Accordion):

<?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代码:

Here's my AJAX code that I am using to insert:

$.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

And here's the 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将数据发布到数据库中.

After selecting the region you are calling the $.ajax to post the data into the database.

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

Once the ajax request is successfully executed. Then return the success message, total amt, shipping zone etc in json format.

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

Then parse JSON and do the calculation and place the values in appropriate elements.

您可以使用sessionstorage()方法以相同的方式在客户端上更新会话.

In the same way you can update the session on the client end by using sessionstorage() method.

示例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.
});

这篇关于PHP-更新购物车总额而无需刷新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆