【问题标题】:Update Cart With Jquery / Ajax使用 Jquery / Ajax 更新购物车
【发布时间】:2012-10-11 12:29:18
【问题描述】:

我刚刚建立了购物车here 添加到购物车按钮可以完美地将产品添加到购物车,而无需刷新整个页面。

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

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

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

这里是 writeShoppingCart() 函数

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(); 重新加载?

【问题讨论】:

  • 是为了向用户展示更新购物车
  • 下次将您的问题标记为 PHP。 :-)

标签: php jquery ajax cart


【解决方案1】:

您在打开&lt;?php 标签后忘记添加session_start() 所以我认为你这样做时引用了一个空变量:$_SESSION['cart'] = $cart; $_SESSION['cart'] 保持为空 要使用会话变量,您总是必须将 session_start 放在页面顶部;

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

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

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

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

//动态改变项目表的代码;

$(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”返回整个网页,如果您在 jAlert() 函数上方添加 alert(resp); 行,您会注意到这一点。
它不应返回整个网页,因此不应像打印其他页面一样将其打印到浏览器!只有 writeShoppingCart() 的结果应该被回显,没有别的!

【讨论】:

  • 您好,谢谢。目前购物车没有问题。我们想要的是,在将产品添加到购物篮后自动更新购物车内容。
  • 好吧,但是如果您完全阅读我的回答,您会注意到我指出您的发布页面正在返回整个网页,它应该只返回 writeShoppingCart() 的内容,在您添加之后我添加到答案中的代码
猜你喜欢
  • 1970-01-01
  • 2016-10-09
  • 1970-01-01
  • 2018-07-29
  • 1970-01-01
  • 1970-01-01
  • 2017-01-11
  • 1970-01-01
  • 2012-07-21
相关资源
最近更新 更多