简体   繁体   English

ajax提交停留在同一页上+更新购物车中产品的数量

[英]ajax submit stay on same page + update quantity of product in cart

Hello I use ajax to stay on the same page after sumbit form. 您好,我使用ajax在sumbit形式后停留在同一页上。 and if success I will use ajax load to load other page that has a text about quantity of product. 如果成功,我将使用ajax加载来加载其他包含有关产品数量文本的页面。 the problem is the if I first submit, the quantity +1/ second submit the quantity +2 (so total 3 now) / third submit, the quantity +3 (so total 6 now). 问题是如果我先提交,数量为+1,第二次提交数量为+2(现在总数为3)/第三次提交数量为3(所以总数现在为6)。 I want it to +1 only 我只希望它+1

I have 3 page. 我有3页。

product.php page (have a products and button add-to-cart(submit) ) I use ajax here product.php页面(有一个产品和按钮添加到购物车(提交))我在这里使用ajax

$(document).ready(function() {
    $('.add-to-cart').on('click', function() {

        $(document).on('submit', '.myForm', function(e) {
            $.ajax({
                url: $(this).attr('action'),
                type: $(this).attr('method'),
                data: $(this).serialize(),
                success: function(html) {
                    $("#outside_cart_text").load("<?php echo base_url();?>shopping/ajax_text.php", function(responseTxt, statusTxt, xhr) {
                        if (statusTxt == "success")

                            if (statusTxt == "error")
                            alert("Error: " + xhr.status + ": " + xhr.statusText);
                    });
                }
            });
            e.preventDefault();
        });

    });
});

A page that ajax load if success shopping/ajax_text.php 如果购物成功,ajax加载的页面/ajax_text.php

<?php echo $_SESSION('qty_type'); ?> type <?php echo $_SESSION('qty_product');?> 

The cart page - the first page(product.php) form will submit to this page. 购物车页面-第一页(product.php)表单将提交到此页面。 and this page has code logic about add item to cart. 并且此页面具有关于将商品添加到购物车的代码逻辑。 so I create session in this page 所以我在此页面中创建会话

$_SESSION['qty_type'] = $sizeof_cart_array;
$_SESSION['qty_product'] = $total_item;

why you want to call on click and on submit as well at the same time ? 为什么要同时点击和提交? Just invoke only on click and handle it. 只需在单击时调用即可处理。

$(document).ready(function() { 
$('.add-to-cart').on('click', function() {

$(document).on('submit', '.myForm', function(e) {<br/> /// code goes here

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM