简体   繁体   English

如何在不刷新产品列表的情况下将产品添加到购物车?

[英]how can i add products to the cart wihtout refreshing the product list?

I made a simple shopping cart function but I have 2 problems now.我做了一个简单的购物车 function 但我现在有两个问题。 1 is my product list is so big and my cart and my product list are in same page. 1 是我的产品列表很大,我的购物车和我的产品列表在同一页。 When visitor click on add button its take too long time to reload how can make it work without refreshing the product page I mean add the product to cart without the refreshing the product page?当访问者单击添加按钮时,它需要很长时间才能重新加载如何在不刷新产品页面的情况下使其工作我的意思是在不刷新产品页面的情况下将产品添加到购物车? And my second problem is I want a product's stock update function how can I make a product update function form that cart function?我的第二个问题是我想要一个产品的库存更新 function 我怎样才能从购物车 function 进行产品更新 function? Here is my code这是我的代码

<?php 
session_start();

$page = 'producst.php';


// my add function
if (isset($_GET['add'])) {
    $quantity = mysql_query('SELECT id, quantity FROM products WHERE id='.mysql_real_escape_string((int)$_GET['add']));
    while ($quantity_row = mysql_fetch_assoc($quantity)) {
        if ($quantity_row['quantity']!=$_SESSION['cart_'.(int)$_GET['add']]){
            $_SESSION['cart_'.(int)$_GET['add']]+='1';
        }
    }
    header('Location: '.$page); 
}


// my product list function
function products(){
    $result = '';
    $get = mysql_query('SELECT id, name, description, price FROM products WHERE quantity > 0 ORDER BY id DESC');
    if (mysql_num_rows($get)==0) {
    }else{
         while ($get_row = mysql_fetch_assoc($get)) {
             $result .= '<p>'.$get_row['name'].'<br />'.$get_row['description'].'<br />&pound;'.$get_row['price'].' <a href="cart.php?add='.$get_row['id'].'">add</a></p>';
             }
    }
    return $result;
}

// my cart function
function cart() {
    $result = '';
    foreach($_SESSION as $name => $value){
        if ($value>0) {
            if (substr($name, 0, 5)=='cart_') {
                $id = substr($name, 5, (strlen($name)-5));
                $get = mysql_query('SELECT id, name, price FROM products WHERE id='.mysql_real_escape_string((int)$id));
                while ($get_row = mysql_fetch_assoc($get)) {
                    $sub = $get_row['price']*$value;
                    $result .= $get_row['name'].' x '.$value.' @ &pound;'.$get_row['price'].' = &pound;'.$sub.'<br />';
                }
            }
        }
    }
    return $result;
}
?>

You'd have to use JavaScript and AJAX to send requests to your PHP script without reloading the page.您必须使用 JavaScript 和 AJAX 将请求发送到您的 PHP 脚本而无需重新加载页面。 PHP runs on your server, while JavaScript runs on and interacts with the browser. PHP 在您的服务器上运行,而 JavaScript 在浏览器上运行并与浏览器交互。

Do some research about AJAX (look at the jQuery framework if you'd like).对 AJAX 进行一些研究(如果您愿意,请查看 jQuery 框架)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WooCommerce-如何使用具有相同购物车按钮的Ajax将多种不同的产品添加到购物车? - WooCommerce - How can I add multiple, different products to cart using ajax with same add-to-cart-Button? 如何隐藏变量产品的“添加到购物车”,但保持产品变化可见 - How to hide 'Add To Cart' for variable products, but keep product variations visible Magento | 我如何在自定义模块中的购物车中添加产品? - Magento | How i can add a product in shopping cart in a custom module? 如何将每个产品作为新商品添加到购物车 - How can I add every product as a new item into cart 我如何直接将特定产品添加到Wordpress中的持久购物车 - How can i directly add particular product to persistent cart in wordpress 如果还添加了特定产品,如何从 WooCommerce 的购物车中删除其他产品? - How can I remove other products from my cart in WooCommerce if a specific product is also added? 如何在购物车中添加两个产品?点击添加到购物车按钮? - How can i add two product in cart onlclick add to cart buton? 当用户离开数据库的“购物车”页面时,如何保存购物车 - How can I keep a shopping cart populated when the user navigates away from the 'cart' page, wihtout using a database 我如何才能将Magento添加到购物车ajax推送正确的网址并将产品添加到购物车? - How can I get Magento add to cart ajax push the right url and add the product to cart? Woocommerce添加到购物车产品仅限4种特定产品 - Woocommerce Add to cart product Restriction to 4 specific products
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM