php - 添加项目并停留在当前页面 - 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>
但是还是不行,点击按钮后,产品被添加了,但是我们并没有停留在同一页面并进入购物篮。
我将不胜感激任何帮助
亚当
解决方案
您可以使用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" />
推荐阅读
- python - 如何在具有总和限制的组内密集排名?
- angularjs - 我想检测浏览器语言并将该浏览器语言用作 angularjs 应用程序语言
- python - 如何生成具有随机值的布尔数组?
- python - PyInstaller:UnicodeDecodeError:“gbk”编解码器无法解码位置 180 中的字节 0xae:非法多字节序列
- javascript - 无法使用 mongodb 读取未定义的 MVC 模型 Web 服务的属性
- ios - Apple 地图图块的 URL
- https - 如何使用 JMeter 加载测试 https
- python - Python(flask)——如何在没有“for循环”的情况下从数据中获取值
- javascript - Puppeteer 不关闭浏览器
- bison - yyparse 如何 *repeatedly* 调用 yylex?