刷新页面产品后自动添加到购物车 [英] After refreshing the page product adding automatically in cart

查看:296
本文介绍了刷新页面产品后自动添加到购物车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为正在运行的购物车编写代码.但是,当我刷新页面时,它会自动向购物车中添加产品.

I am trying to make the code for a shopping cart, which is working. However, when I refresh the page it's adding a product to my cart automatically.

例如,我在我的网站上有3个产品,分别是Apple, Banana, Orange. 我单击Apple,它已以QTY 1添加到我的购物车,并且URL显示

For example, I have 3 products on my website, which are Apple, Banana, Orange. I click on Apple and it's added to my cart with QTY 1 and the URL is showing

`mydomain.com/my-cart?action=addcart&p_id=FylvGt6Yyb6n%2BzTXcJHwjBawOY%2Fw3QSZxF7rdUJLqhA%3D#`

现在,如果我刷新页面,则它将在购物车中添加另一个Apple(数量2).然后,如果我再次刷新页面,它将添加另一个Apple(数量3),依此类推.我不知道为什么会这样.它是在SESSION中添加的.

Now if I refresh the page then it's adding another Apple to my cart (QTY 2). Then if I refresh the page again it adds another Apple (QTY 3) and so on. I don't know why this is happing. It's adding to SESSION.

您能帮我吗?

下面是我的购物车代码.

Below Is my cart code.

    $action = isset($_GET['action'])?$_GET['action']:"";
       $p_id=$conn->real_escape_string($_GET['p_id']);
       $decrypted_p_id = decryptIt($p_id);
    //Add to cart
    if($action=='addcart') {
        //Finding the product by code
     $query = "SELECT p_unique_id, p_images,p_name, p_currentprice FROM products WHERE p_id=?";
    if ($stmt = $conn->prepare($query)) {
        $stmt->bind_param("i", $decrypted_p_id);
        $stmt->execute();
        $stmt->bind_result($p_unique_id,$p_images, $p_name, $p_currentprice);
        $stmt->fetch();

}
        $currentQty = $_SESSION['products'][$decrypted_p_id]['qty']+1; //Incrementing the product qty in cart
        $_SESSION['products'][$decrypted_p_id] =array(
                                            'qty'=>$currentQty,
                                            'p_unique_id'=>$p_unique_id,
                                            'p_images'=>$p_images,
                                            'p_name'=>$p_name,
                                            'p_currentprice'=>$p_currentprice
                                        );
        $product='';
       // header("Location:cart.php");
    }

展示产品

  <?php if(!empty($_SESSION['products'])):
   foreach($_SESSION['products'] as $key=>$product):?>
    /*some code here*/
    endforeach;?>
    <?php endif;?>

此处已编辑代码,由ADyson建议

Edited code here Suggested by ADyson

    if (isset($_POST['submit'])) {
        $action=$conn->real_escape_string($_POST['action']);
       $decrypted_p_id=$conn->real_escape_string($_POST['p_id']);
// whole code here

推荐答案

如果仅在完全相同的页面上单击刷新",则URL中仍会包含action=addcart等.因此,不可避免的是,在页面加载时,它将再次运行该操作,然后将项目再次添加到购物车中.

If you simply click refresh on the exact same page then action=addcart etc. is still in the URL. Therefore inevitably it runs that action again when the page loads, and adds the items to the cart again.

这样的添加"动作最好作为POST请求来完成,部分原因是出于语义原因(它是发送"数据而不是获取"数据),部分是避免此类麻烦.理想情况下,GET请求不应引起应用程序状态的任何改变.

An "add" action like that would be better done as a POST request, partly for semantic reasons (it's "sending" data rather than "get"ting it) and partly to avoid annoyances like this. Ideally a GET request should not cause any change of state in the application.

代替

<a href="my-cart?action=addcart&p_id=<?php echo $p_user_id;?>">Add to cart</a>

您可以执行以下操作:

<form action="my-cart" method="POST">
  <button type="submit">Add to cart</button>
  <input type="hidden" name="action" value="addcart"/>
  <input type="hidden" name="p_id" value="<?php echo $p_user_id;?>"/>
</form>

如果愿意,可以使用一些CSS使按钮看起来更像旧的超链接.

You can use some CSS to make the button appear more like your old hyperlink, if you wish.

然后,在您的PHP购物车代码中,只需将所有对$_GET的引用替换为$_POST,即可从POST请求中读取值.

Then, in your PHP cart code, simply replace all references to $_GET with $_POST instead, to read the values from the POST request.

这将具有以下优点:变量不会保存到URL中,因此,如果有人尝试刷新URL,它将不会基于这些变量自动重复相同的操作.

This will have the advantage that the variables are not saved into the URL and therefore if someone tries to refresh the URL it will not automatically repeat the same action based on those variables.

您还可以考虑通过AJAX发送数据以添加购物车商品,这样它根本就不需要回发,并且通常可以带来更流畅的用户体验-如果您现在查看的是大多数购物网站,通常他们做什么.

You could also look into sending the data to add cart items via AJAX, so that it doesn't require a postback at all and usually results in a smoother user experience - if you look at most shopping websites now, that is generally what they do.

这篇关于刷新页面产品后自动添加到购物车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆