dongmeng4742 2015-08-27 01: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 01: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;
            }
        });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?