求助,商品加入购物车js提交添加

2016-07-07 15:01 来源:www.chinab4c.com 作者:ecshop专家

商品加入购物车,商品数量什么是用 js传递的,想再自己加一个传递的值,在flow.php 打印$_POST['goods'],提示例外被抛出不被接住。js代码修改如下flow.php需要改动?才能取到新加进去的值,测试n边不成功,高人帮帮忙啊

function addToCart(goodsId, parentId)
{
var goods= new Object();
var spec_arr= new Array();
var fittings_arr = new Array();
var number = 1;
var dihuan;
var formBuy= document.forms['ECS_FORMBUY'];
// 检查是否有商品规格
if (formBuy)
{
spec_arr = getSelectedAttributes(formBuy);
if (formBuy.elements['number'])
{
number = formBuy.elements['number'].value;
}
if (formBuy.elements['dihuan'])
{
dihuan = formBuy.elements['dihuan'].value;
}
}
goods.spec= spec_arr;
goods.goods_id = goodsId;
goods.number= number;
goods.dihuan= dihuan;
goods.parent= (typeof(parentId) == "undefined") ? 0 : parseInt(parentId);
Ajax.call('flow.php?step=add_to_cart', 'goods=' + goods.toJSONString(), addToCartResponse, 'POST', 'JSON');
}

回答:
toJSONString()方法你看过没有?

2# ouyoyo

需要动这个吗?现在一头雾水啊 ,,,,只需要传递过去的值能在flow.php够物车页面取到就完事了

我也需要啊。。

在线等待啊,高人帮帮忙啊

那有什么问题啊?

你的意思是想把dianhua这个参数的东西也加入到购物车?
Ajax.call()前加
goods.dianhua = '333333333333';
alert( goods.toJSONString());
就能看到类似的东西,

存入购物车里的函数是 if (addto_cart($goods->goods_id, $goods->number, $goods->spec, $goods->parent))

要是没有错的话,$goods->dianhua就是你想要的值,

7# ouyoyo

谢谢.太感谢了,但是dihuan在商品属性页面是
<input type="radio" value= "2" name="dihuan" >
<input type="radio" value="1" name="dihuan" >
alert 33333可以出来

但是我要取到 name="dihuan"的直 就没有

if (formBuy.elements['dihuan'])
{
dihuan = formBuy.elements['dihuan'].value;
}

这段有问题吗

如果是多选是要写一个函数把值取出来才行的,你可以参考一下 spec_arr = getSelectedAttributes(formBuy);

我也遇到这个问题了,这是我的解决方法和问题描述:
uncaught exception: [Exception... "'transport.js/parseResult() error: can't parse to JSON. <br /> <b>Warning</b>: number_format() expects parameter 1 to be double, string given in <b>D:\wamp\www\ecshop\includes\lib_common.php</b> on line <b>965</b><br /> {"error":6,"message":[{"attr_type":"1","name":"\u989c\u8272","attr_id":"185","values":[{"label":"\u9ed1\u8272","price":"","format_price":"\uffe5\u5143","id":"196"},{"label":"\u91d1\u8272","price":"10","format_price":"\uffe510.00\u5143","id":"197"},{"label":"\u767d\u8272","price":"20","format_price":"\uffe520.00\u5143","id":"198"}]}],"content":"","goods_id":"19","parent":0}' when calling method: [nsIDOMEventListener::handleEvent]" nsresult: "0x8057001e (NS_ERROR_XPC_JS_THREW_STRING)" location: "JS frame :: chrome://firebug/content/spy.js :: callPageHandler :: line 744" data: no]

查了下代码,在includes/lib_common.php中964行
$price = number_format($price,2, '.', '');改为下面的方式就可以工作了
$price = number_format((double)$price,2, '.', '');