首页 > 解决方案 > 添加项目并停留在当前页面 - php、ajax、jquery

问题描述

我正在使用谷歌翻译,所以可能会出现错误。

我的问题:添加项目并停留在当前页面

我尝试使用下面链接中的建议,但没有成功:-(

添加项目并停留在当前页面

脚本代码:

    <?php 
if( !defined( 'CUSTOMER_PAGE' ) )
  exit;
require_once DIR_SKIN.'_header.php'; // include design of header
?>
<div id="product">
<?php
if( isset( $aData['sName'] ) ){ // displaying product content ?>
  <script type="text/javascript">
    var sTitle = "<?php echo $aData['sName']; ?>";
    var fPrice = Math.abs( "<?php echo $aData['mPrice']; ?>" );
  </script><?php

  if( isset( $aData['mPrice'] ) || isset( $aData['sAvailable'] ) ){ // displaying box with price, basket and availability - START
    echo '<div id="box">';
      if( isset( $aData['mPrice'] ) && is_numeric( $aData['mPrice'] ) ){?>
        <div id="price"><em><?php echo $lang['Price']; ?>:</em><strong id="priceValue"><?php echo $aData['sPrice']; ?></strong><span><?php echo $config['currency_symbol']; ?></span></div><?php
      }
      elseif( !empty( $aData['mPrice'] ) ){?>
        <div id="noPrice"><?php echo $aData['sPrice']; ?></div><?php
      }
      if( isset( $aData['sAvailable'] ) ){?>
        <div id="available"><?php echo $aData['sAvailable']; ?></div><?php
      }
      if( isset( $aData['mPrice'] ) && is_numeric( $aData['mPrice'] ) && !empty( $config['basket_page'] ) && isset( $oPage->aPages[$config['basket_page']] ) ){?>
        <form action="<?php echo $oPage->aPages[$config['basket_page']]['sLinkName']; ?>" method="post" id="addBasket" class="form">
          <fieldset>
            <legend><?php echo $lang['Basket_add']; ?></legend>
            <input type="hidden" name="iProductAdd" value="<?php echo $aData['iProduct']; ?>" />
            <input type="hidden" name="iQuantity" value="1" />
            <input type="submit" value="<?php echo $lang['Basket_add']; ?>" class="submit" />
          </fieldset>
        </form><?php
      }
    echo '</div>';
  } // displaying box with price, basket and availability - END
}
?>
</div>

我重写了代码并将其添加到下面:

if( isset( $aData['sName'] ) ){ // displaying product content ?>

 <script type="text/javascript">
    $('#addBasket .submit').click(function() {
    var keyValues = {
        iProductAdd : $(this).parent().find('input[name="iProductAdd"]').val(),
        iQuantity : $(this).parent().find('input[name="iQuantity"]').val()
    };
    $.post('<?php echo $oPage->aPages[$config['basket_page']]['sLinkName']; ?>', keyValues, function(rsp) {
        // make your php script return some xml or json that gives the result
        // rsp will be the response
    });
    return false; // so the page doesn't POST
});
  </script>

但是还是不行,点击按钮后,产品被添加了,但是我们并没有停留在同一页面并进入购物篮。

我将不胜感激任何帮助

亚当

标签: phpjqueryajax

解决方案


您可以使用e.preventDefault()而不是 return false;

<script type="text/javascript">
    $('#addBasket .submit').click(function(e) {
    e.preventDefault();
    var keyValues = {
        iProductAdd : $(this).parent().find('input[name="iProductAdd"]').val(),
        iQuantity : $(this).parent().find('input[name="iQuantity"]').val()
    };
    $.post('<?php echo $oPage->aPages[$config['basket_page']]['sLinkName']; ?>', keyValues, function(rsp) {
        // make your php script return some xml or json that gives the result
        // rsp will be the response
    });

});

编辑1:

你可以再做一件事。而不是输入类型为submit. 保持它,button因为我注意到您正在手动制作 JSON。

所以你的输入提交可以是这样的

<input type="button" value="<?php echo $lang['Basket_add']; ?>" class="submit" />

推荐阅读