购物车到Json

时间:2012-07-30 06:50:59

标签: jquery json

我需要在按钮点击时生成json数据,但我无法获得json
我的购物车演示链接 http://jsfiddle.net/bkw5p/48/

1 个答案:

答案 0 :(得分:1)

在HTML结尾处,在<p class="total"><h2>之间:

<input type="submit" id="submit" value="Make an order" />

$(function() { ... })内的JS中:

$('#submit').bind('click',function(){
    if (!$('table#cartcontent1 tr').length) {
        alert('You have no Items in Your Cart');
    } else {

        var result = new Array();
        var name, qty, price;
        $('table#cartcontent1 tr').each(function(){
            name = $(this).find('td[data-bind="text:name"]').text();
            qty = $(this).find('input[data-bind="value:qty"]').val();
            price = $(this).find('td[data-bind="text:price"]').text();
            result.push( {'name' : name, 'qty' : qty, 'price' : price } );
        })

        $.ajax({
            type:"POST",
            url: 'ordering.php',
            data: {data: result},
            success: function(msg){
                // do something when success
            }
        });
    }
    return false;
})

在PHP中它就像:

$data = $_POST['data'];
foreach ($data as $val) {
    echo $val['name']; // Feeling
    echo $val['price']; // 25
    echo $val['qty']; // 3
}
相关问题