dongmeng4742 2015-08-27 09:00
浏览 24
已采纳

如何在移动中更新会话数组中的数量?

I want to capture value keyed in textbox for quantity in the shopping cart while they type. I used jquery's key up for this.Then I want to save the new value in the session for quantity. I'm getting the value but unable to save it. It returns null after refreshing the page.

Html

<input type="text" size="4" class="qty" value="<?php echo $v['quantity'];?>" id="<?php echo $k;?>"/>

Ajax

$("input[class=qty]").keyup(function()
     {
        var data;
         var newVal = $(this).val();

         var id = this.id;
         console.log(id+" "+newVal);
         $.ajax({
              type: "GET",
              dataType: "text",
              url: "updateCart.php?id="+id+"&&value"+newVal, //Relative or absolute path to response.php file
              data: data,
              success: function(data) {
                  console.log(data);
                 newVal=data;
              }
            });

     });

updateCart.php

<?php
session_start();
$id = $_GET['id'];//say 2
$new_value =$_GET['value'];//say 16
foreach($_SESSION['cart'] as $k=>$v)
{

    $k;
    $v["id"];
    $v["quantity"];
    if($id == $k)
    {
        //assign new value to the quantity
        $_SESSION['cart'][$id]['quantity']=$new_value;
        //print_r($_SESSION['cart'][1]['quantity']);
    }

}
//echo json_encode($new_value);
//var_dump($_SESSION['cart']);

this can display session['cart'] with updated value if I test in this page ..but when passed to originated page via ajax, returns null.

echo $_SESSION['cart'][$id]['quantity'];
?>
  • 写回答

1条回答 默认 最新

  • duanputian5341 2015-08-27 09:14
    关注

    You left out the = between &value and + newVal. But I recommend you let jQuery do this automatically by using the data: option.

        data = { id: id, value: newVal };
        $.ajax({
            type: "GET",
            dataType: "text",
            url: "updateCart.php", //Relative or absolute path to response.php file
            data: data,
            success: function(data) {
                console.log(data);
                newVal=data;
            }
        });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加