刷新会话和数组而不刷新页面 [英] Refresh Session and Array without refreshing page

查看:68
本文介绍了刷新会话和数组而不刷新页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望这不是一个愚蠢的问题.

just hope this is not a stupid question.

我有一个电子商务网站,当用户将商品添加到购物车时.在页面顶部,我具有此功能,该功能可以对数组进行计数,然后向用户显示购物车中有多少个物品.

I have an eCommerce site and when user adds an item to cart. On top of the page I have this function which count the array and then shows the user how many item is in cart.

然后,我将购物车总价存储在一个会话中,以也在页面顶部显示.

Then I am store the total cart price in a session to show show on top of the page as well.

我正在使用Jquery将商品添加到购物车,但是问题是您必须刷新页面到,这样您才能看到购物车项目总数和购物车总价格(在页面顶部.不是购物车页面.所以不要把它混在一起).

I am using Jquery to add an item to cart but the problem is you have to refresh the page to so that you can see the total cart item and total cart price(On top of the page. Not the cart page. So don't get it mix it).

我使用 location.reload(); 重新加载Jquery,但这刷新了自己的页面.

I used location.reload(); to reload in the Jquery but this refresh the own page.

要显示用户在购物车中拥有的商品数,请 count

To show the number of item the user have in cart I did count

<?php回声计数($ _SESSION ["cart_array"])?>

并为总价回声会话

<?php echo $ _SESSION ['smallcart'];?>

如何使用PHP或任何其他形式刷新 count smallcart 会话,而无需刷新页面.

How can I use PHP or any other form to refresh the count and the smallcart session without refresh the page.

这些都在我的pageheader.php中

Those those are in my pageheader.php

<?php include realpath(dirname(__ FILE __)."/../../pageheader.php");?>

它们是我可以刷新 pageheader.php 而不刷新页面 body 的一种方法.

is they a way i can refresh the pageheader.php without refresh the page body.

推荐答案

您可以使用ajax进行操作,如下例所示.首先,我们需要一个php文件,它将创建这些会话并与ajax通信.让我们将此文件称为 ajax_cart.php .其中可能包含以下代码.

You can do it using ajax as shown in example below. First we need a php file which will create those session and communicate with ajax. Let call this file ajax_cart.php. Which may have the following codes.

    $new_cart_item=$_REQUEST['new_cart_item'];
$new_total=$_REQUEST['new_total'];
$cart_array=array();
//for cart arrays
$item_count=0;
if(isset($_SESSION['cart_array'])){
$cart_array=$_SESSION['cart_array'];
$cart_array[]=$_REQUEST['new_cart_item'];
$_SESSION['cart_array']=$cart_array;
$item_count=count($_SESSION["cart_array"]);
else{
$cart_array[]=$_REQUEST['new_cart_item'];
$_SESSION['cart_array']=$cart_array;
$item_count=count($_SESSION["cart_array"]);
}

//for smart cart
$total_price=0;

$_SESSION['smartcart']=$_REQUEST['new_total'];
$total_price=$_SESSION['smartcart'];
$data='{"cartcount":"'.$item_count.'","totalprice":"'.$total_price.'"}';
echo $data;

然后,我们可以添加ajax进行更新,并从该文件中获取结果.从以下代码中获取.

Then we can add ajax to update and get the result from that file. from the following codes.

//ajax side
function my_ajax(url,data,method,datatype){
    $.ajax({
    type: method,
    url: url,
    data: data,
    dataType: datatype,
    success: function(data) {
    ajaxResultHandler(data,datatype);
    },
    error: function(error, status, desc) {
        console.log(error);

    }
});
}

function ajaxResultHandler(data,datatype){
//here you can put any function to use the given data
var cart_count=data.cartcount;
var total_price=data.totalprice;
$("div.cart_count").html(cart_count); //your element you display item count
$("div.total_price").html(total_price); //your element you display total price.
}

以下是该ajax函数的用法.

The following is the usage of that ajax function.

$('.add_to_cart').live("click",function(e) {
    e.preventDefault();

    //get required data from elements or form. perform addation of price.
    var total_price=0; //put calculated new total price
    var added_item=""; //put the new added item
    //call the ajax function
    my_ajax("ajax_cart.php",{"new_total":total_price,"new_cart_item":added_item},"post","json");
    });

尝试这种方法,希望能解决您的问题.

Try this approach hope it will solve your problem.

这篇关于刷新会话和数组而不刷新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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