简体   繁体   中英

After refreshing the page product adding automatically in cart

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.

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#`

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.

Would you help me in this?

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");
    }

Displaying product

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

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

i found this code:

$currentQty = $_SESSION['products'][$decrypted_p_id]['qty']+1;

this code always run when u reload the page, give some condition if you want to add manually

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.

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.

Instead of

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

you can do something like:

<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>

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

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM