ajax更新购物车数量

把我最近写的一个代码贴出来   可以参考 但不一定适用。

   
一、首先我把这个输入框的代码贴出来

  1. <INPUT class="input-text f-input"  οnblur="changePrice(document.getElementById('goods_number_{$goods.rec_id}').value,{$goods.rec_id})" name="goods_number[{$goods.rec_id}]" id="goods_number_{$goods.rec_id}" value="{$goods.goods_number}"  id="ECS_FORMBUY">

复制代码

二、次 页面加入的javascript 也就是changeprice函数

  1. <script>
  2. /**
  3. * 点选可选属性或改变数量时修改商品价格的函数
  4. */
  5. function changePrice(number,rec_id)
  6. {
  7.   //v//ar attr = getSelectedAttributes(document.forms['ECS_FORMBUY']);
  8. // var qty = document.forms['ECS_FORMBUY'].elements['number'].value;
  9.   
  10.    Ajax.call('flow.php', 'step=update_group_cart&rec_id=' + rec_id +'&number=' + number, changePriceResponse, 'GET', 'JSON');
  11. }
  12. /**
  13. * 接收返回的信息
  14. */
  15. function changePriceResponse(res)
  16. {
  17.   if (res.error > 0)
  18.   {
  19.     document.getElementById('sysmsg_error').innerHTML = res.content;
  20.     document.all.sysmsg_error.style.display='';
  21.   }
  22.   else
  23.   {
  24.     if(document.all.sysmsg_error.style.display=='')
  25.   {
  26.    document.all.sysmsg_error.style.display='none';
  27.   }
  28.   document.getElementById('subtotal_'+res.rec_id).innerHTML = res.subtotal;//商品总价
  29.   document.getElementById('cart_amount').innerHTML = res.cart_amount;//购物车团购商品价
  30.   
  31.   }
  32. }
  33. </script>
  34. <script>
  35. /**
  36. * 点选可选属性或改变数量时修改商品价格的函数
  37. */
  38. function changePrice(number,rec_id)
  39. {
  40.   //v//ar attr = getSelectedAttributes(document.forms['ECS_FORMBUY']);
  41. // var qty = document.forms['ECS_FORMBUY'].elements['number'].value;
  42.   
  43.    Ajax.call('flow.php', 'step=update_group_cart&rec_id=' + rec_id +'&number=' + number, changePriceResponse, 'GET', 'JSON');
  44. }
  45. /**
  46. * 接收返回的信息
  47. */
  48. function changePriceResponse(res)
  49. {
  50.   if (res.error > 0)
  51.   {
  52.     document.getElementById('sysmsg_error').innerHTML = res.content;
  53.     document.all.sysmsg_error.style.display='';
  54.   }
  55.   else
  56.   {
  57.     if(document.all.sysmsg_error.style.display=='')
  58.   {
  59.    document.all.sysmsg_error.style.display='none';
  60.   }
  61.   document.getElementById('subtotal_'+res.rec_id).innerHTML = res.subtotal;//商品总价
  62.   document.getElementById('cart_amount').innerHTML = res.cart_amount;//购物车团购商品价
  63.   
  64.   }
  65. }
  66. </script>
复制代码


然后我把请求的php处理 程序也贴出来


  1. elseif($_REQUEST['step'] == 'update_group_cart')
  2. {
  3.     /*------------------------------------------------------ */
  4.     //-- 西安php服务中心团购更新购物车
  5.     /*------------------------------------------------------ */
  6.     include_once('includes/cls_json.php');
  7.     $json = new JSON();
  8.     $result = array('error' => '', 'content' => '');
  9. $rec_id = $_GET['rec_id'];
  10. $number = $_GET['number'];
  11.   
  12.   /*取的团购信息*/
  13.     $group_buy = group_buy_info($_SESSION['extension_id'], $number);

  14. /*判断输入是否合法*/
  15. if(!is_numeric($number))
  16. {
  17.           $result['error'] = '1';
  18.        $result['content'] ='请输入合法数量';
  19.        die($json->encode($result));

  20. }
  21.     if ($group_buy['restrict_amount'] > 0 && $number > ($group_buy['restrict_amount'] - $group_buy['valid_goods']))
  22.     {
  23.           $result['error'] = '1';
  24.     $restrict_amount =  $group_buy['restrict_amount'] - $group_buy['valid_goods'];
  25.     $result['content'] ='您最多可买'.$restrict_amount.'件';
  26.        die($json->encode($result));
  27.    }
  28.     $sql = "UPDATE " . $GLOBALS['ecs']->table('cart') . " SET goods_number = '$number' WHERE rec_id = $rec_id";
  29.    $GLOBALS['db']->query($sql);
  30. /*计算此订单总价*/
  31. $subtotal = $GLOBALS['db']->getONE("select goods_price * goods_number AS subtotal from ".$GLOBALS['ecs']->table('cart')." where rec_id = $rec_id");
  32.    /*购物车团购商品总金额*/
  33.    $cart_amount = cart_amount('',$_SESSION['flow_type']);
  34. $result['subtotal'] = price_format($subtotal, false);
  35. $result['cart_amount'] = price_format($cart_amount, false);
  36. $result['rec_id'] = $rec_id;
  37.     die($json->encode($result));
  38. }
复制代码

转载于:https://my.oschina.net/u/1038053/blog/467626

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值