Refresh Session and Array without refreshing page

蹲街弑〆低调 提交于 2021-01-28 06:54:06

问题


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.

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

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

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

<?php echo count($_SESSION["cart_array"])?>

and for the total item price echo the session

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

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

Those those are in my pageheader.php

which I <?php include realpath(dirname(__FILE__)."/../../pageheader.php"); ?>

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


回答1:


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;

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

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.



来源:https://stackoverflow.com/questions/18454560/refresh-session-and-array-without-refreshing-page

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!