[英]ajax submit stay on same page + update quantity of product in cart
您好,我使用ajax在sumbit形式后停留在同一頁上。 如果成功,我將使用ajax加載來加載其他包含有關產品數量文本的頁面。 問題是如果我先提交,數量為+1,第二次提交數量為+2(現在總數為3)/第三次提交數量為3(所以總數現在為6)。 我只希望它+1
我有3頁。
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();
});
});
});
如果購物成功,ajax加載的頁面/ajax_text.php
<?php echo $_SESSION('qty_type'); ?> type <?php echo $_SESSION('qty_product');?>
購物車頁面-第一頁(product.php)表單將提交到此頁面。 並且此頁面具有關於將商品添加到購物車的代碼邏輯。 所以我在此頁面中創建會話
$_SESSION['qty_type'] = $sizeof_cart_array;
$_SESSION['qty_product'] = $total_item;
為什么要同時點擊和提交? 只需在單擊時調用即可處理。
$(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.