使用Jquery/Ajax更新购物车 [英] Update Cart With Jquery / Ajax

查看:51
本文介绍了使用Jquery/Ajax更新购物车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在此处构建购物车添加到购物车"按钮可以完美地将产品添加到购物车,而无需刷新整个页面.

I've just build shopping cart here The add to cart button work perfectly adding the product to the cart without refreshing the entire page.

我的问题是,如何在点击添加到购物车链接后使购物篮更新内容,而不刷新页面

My question is, how to make the basket update the content after hit the add to cart link without refresh the pages

我有用于处理将产品添加到购物车并显示购物车内容的代码.

I have code to handle adding a product to the cart and to show the cart content.

<?php
if($what=="addtocart")
{
     if ($cart)
     {
        $cart .= ','.$_GET['product_id'];
     } 
     else 
     {
            $cart = $_GET['product_id'];
     }
     $_SESSION['cart'] = $cart;
}
echo writeShoppingCart();
?>

这是writeShoppingCart()函数

and here are the writeShoppingCart() function

function writeShoppingCart() {
    $cart = $_SESSION['cart'];
    if (!$cart) {
        return '<p>You have no items in your shopping cart</p>';
    } else {

echo "<table class=table cellpadding=5 cellspacing=0 width=87% border=0>";
echo "<tr class=bold>";
echo "<td width=65>ID Product</td>";
echo "<td>Pattern</td>";
echo "<td>Inst Type</td>";
echo "</tr>";
    include "config.php";
    global $dbhost,$dbusername,$dbpassword;
    $id_mysql = mysql_pconnect($dbhost,$dbusername,$dbpassword);
    mysql_select_db($dbname, $id_mysql);
    $cart = $_SESSION['cart'];
        $items = explode(',',$cart);
        $contents = array();
        foreach ($items as $item) {
            $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
        }
        foreach ($contents as $id=>$qty) {
        $view2 = "SELECT * FROM $table_product WHERE id='$id'";
        $result2 = mysql_query($view2);


        while
            ($baris=mysql_fetch_row($result2))
            {
echo "<tr>";
echo "<td>$baris[1]</td>";
echo "<td>$baris[2]</td>";
echo "<td>$baris[3]</td>";
echo "</tr>";
            }   

        }
        echo "</table>";
        echo "<span class=\"go_cart\">&raquo;&nbsp;<a href=\"cart.php\">View Complete Basket</a></span>";
    }
}

在将商品添加到购物车后,是否有任何线索可以使 echo writeShoppingCart(); 重新加载?

is there any clue to make the echo writeShoppingCart(); reload after adding a product to the cart?

推荐答案

打开<?php 标签后,您忘记添加 session_start()所以我认为您在执行此操作时引用的是一个空变量: $ _ SESSION ['cart'] = $ cart; $ _SESSION ['cart']保持为空要使用会话变量,您总是必须将session_start放在页面顶部;

you forgot to add session_start() after opening your <?php tag so I think you're referencing an empty variable when you do this: $_SESSION['cart'] = $cart; $_SESSION['cart'] stays empty to use the session variable you ALWAYS have to put session_start right at the top of the page;

<?php session_start();
   //your stuff here
?>

我现在刚刚签出了您的页面,我注意到您正在使用wordpress,wordpress不支持使用会话变量,因此您必须在主题中寻找接收到第一个控制权的页面,通常是您的主题目录中的header.php:/yourroot/wp-content/themes/yourthemename/header.php ,您必须在该页面顶部的右上方添加代码.

I just checked your page out now and I noticed you're working within wordpress, wordpress doesn't support using session variables, so you'll have to look for the page in your theme that receives first control, it's usually the header.php inside your theme directory: /yourroot/wp-content/themes/yourthemename/header.php, you'll have to add the code above right at the top of that page.

还:我警告了从添加链接"返回的 resp ,它返回了整个网页,您调用的php脚本应该在wordpress之外,最好是在您直接访问的文件中,这样,当返回结果时,您只有要回显的函数的输出

also: I alerted the resp returned from the "add-link" and it returns an entire webpage, the php script you call should be outside of wordpress, prefereably in a file you access directly, so that when the result is returned you have only the output of the function you're echo-ing

如果您仍然有错误,请发表评论

Please comment if you're still having errors

//代码以动态更改项目表;

//code to dynamically change item table;

$(document).ready(function(){

    $('a#add-link').bind("click", function(event) {
        event.preventDefault();
        var url = $(this).attr("href");
        $.get(url, function (resp) {
            jAlert('Product Has Been Added to Shopping Cart');
            //to do the following you have to put your shoppingcart table in a div with the id="shoppingcart"
            $("#shoppingcart").html(resp)
        });
    });
});

PS:因为我会为您完成所有代码,所以您会忘记这一点.
现在,您的帖子页面:"http://ksul.kittenpile.com/product.php"返回了整个网页,如果您在自己的网页上方添加 alert(resp); jAlert()函数.
它不应该返回整个网页,所以不应该像打印其他页面一样将其打印到浏览器中!仅应回写writeShoppingCart()的结果,否则就不会了!

PS: because I think you'll forget about this since I'm doing all your code for you.
right now your post page: "http://ksul.kittenpile.com/product.php" is return an entire webpage, which you will notice if you add the line alert(resp); above your jAlert() function.
It shouldn't return an entire webpage, so it shouldn't be printed to the browser the same way your other pages are printed! only the result of writeShoppingCart() should be echoed and NOTHING ELSE!

这篇关于使用Jquery/Ajax更新购物车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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