传递购物车数据…Ajax或PHP Post


Passing Over Shop Cart Data... Ajax or PHP Post?

首先,感谢一个叫Marcel Gwerder的家伙,他写了我要问的问题的代码。我会写评论或者给他发tweet,但我觉得这个帖子已经死了(人们不会只看旧帖子,对吧?)他们也不能被撞),我不知道如何PM People。

看这段代码

// Shop Stuff
var cart = [];
$(document).ready(function(){  
    var buttonTxt = '';
    $(".buyinfo").click(function() {
        //Store text and id of the selected element
        var txt = $(this).siblings('.shopitemname').text(); 
        var id = $(this).closest('.shopitem').attr('id');
        if(!$(this).hasClass('added')) {
           buttonTxt =  $('.buyinfoname', this).text();
           $('#box_item').text(txt);
           cart[id] = txt;            
           //Change text
           $('.buyinfoname', this).text('Added to cart - Click to remove');
           $(this).addClass('added');
           //Show and hide overlay
           $('#confirmbox').show('normal').delay(2000).fadeOut();
        } else {
           delete(cart[id]);
           $(this).removeClass('added');
           $('.buyinfoname', this).text(buttonTxt);
        }
        console.log(cart);
        alert(cart);
    });
}); 

这是一些Javascript。

现在是一些由评论者推荐的HTML。

<div id="shop">
<a href="checkout.php"><input type="button" value="Go To Checkout" id="checkoutbutton" /></a>
<div class="shopitem">
<p class="shopitemname">Orange Background Color</p>
<div class="buyinfo">
<p class="buyinfoname">Buy - 40 Coins</p>
</div>
</div>
<div class="shopitem">
<p class="shopitemname">Black Background Color</p>
<div class="buyinfo">
<p class="buyinfoname">Buy - 40 Coins</p>
</div>
</div>
<div class="shopitem">
<p class="shopitemname">Green Background Color</p>
<div class="buyinfo">
<p class="buyinfoname">Buy - 40 Coins</p>
</div>
</div>
<div class="shopitem">
<p class="shopitemname">Blue Background Color</p>
<div class="buyinfo">
<p class="buyinfoname">Buy - 40 Coins</p>
</div>
</div>
<div class="shopitem">
<p class="shopitemname">Yellow Background Color</p>
<div class="buyinfo">
<p class="buyinfoname">Buy - 40 Coins</p>
</div>
</div>
<div class="shopitem">
<p class="shopitemname">Purple Background Color</p>
<div class="buyinfo">
<p class="buyinfoname">Buy - 40 Coins</p>
</div>
</div>
</div>
</section>
<div id="confirmbox">
<p>The item was successfully added to your cart</p>
</div>

我想知道如何发送在CART变量的值到一个新的页面,有人可以点击称为"checkout.php"。我想AJAX从jquery或post从PHP,但两者都很难对我(我是一个新手编码),我不想努力工作的东西,可能不会在最后的工作。

还有一个问题要问Marcel(如果他能看到的话)或者任何能理解他代码的人:

为什么没有值进入购物车,我设置了alert(cart),当购物车中应该有值时,它总是显示空的警告框。

请帮助我,祝你有美好的一天。

您可以这样做,我创建了一个工作JS小提琴示例示例。这只是在客户端,你必须编写后端处理脚本,因为我不确定你想做什么(在数据库中保存值等)。

请看下面的JS代码:http://jsfiddle.net/rDgUD/2/

你可以在jQuery中使用。post命令:

$("#checkoutbutton").click(function () {
   $.post("test.php", cart, function (data) {
        //this is the reponse back from your PHP processing page that saved the variables in a database or however you were handling that.
        alert("Data Loaded: " + data);
    });
});

您可以看到我是如何在JS代码中将变量保存到购物车数组中的。使用jquery的.push功能添加数组元素:

cart.push(id);

我还使用以下代码删除了元素:

var removeItem = id; // item to remove
cart.splice($.inArray(removeItem, cart), 1);

Javascript在用户计算机上运行,PHP在服务器上运行。
所以,虽然你可能想使用javascript来收集数据,我很确定你会想用PHP 处理的数据,以避免用户能够干扰它太容易。

"认为你的Ajax代码只是另一个客户端到你的服务器"从安全建议的jquery Ajax数据后?