Jump to content

clear shopping cart content on closing browser or body unload


Recommended Posts

hi all

 

i am looking for a function that clears the contents of the shopping cart on closing the browser and displays an confirm alert before removing the contents.

 

i have a script that clear contents on clicking the clear cart button but i need to have the same option on closing the browser.

 

this is my script

if(isset($_REQUEST['clear_cart_x']))
{
$product_id=$row['product_id'];
$image=$row['image'];
$product_name=$row['product_name'];
$price=$row['price'];
$shipping_cost=$row['shipping_cost'];
$quantity=$row['quantity'];
$total_cost=$row['total_cost'];
$qry="delete from cart_table where unique_id='$unique_id'";
mysql_query($qry);
}

 

vineet

I'm not sure of the limits to javascript's "onunload" .. butyou could try something like:

 

<script type="text/javascript">
function clearCart() {
    // try to make an ajax call to say 'clearcart.php' ..
}
</script>

 

Then just add:

 

<body onunload="clearCart();">

 

When the clearCart() function is called the page will have already unloaded, so I'm not sure if there any limits in what you can do, but worth a shot!

 

Adam

i would have a temp cart that once the user orders it will move the contents to the real cart where your order is saved. then daily automate a script to go in and delete all in the temp cart that is over a day old. this way you can keep your database down.

 

if you try and do this using javascript there are to many reasons why it won't work.

I'm not sure of the limits to javascript's "onunload" .. butyou could try something like:

 

<script type="text/javascript">
function clearCart() {
    // try to make an ajax call to say 'clearcart.php' ..
}
</script>

 

Then just add:

 

<body onunload="clearCart();">

 

When the clearCart() function is called the page will have already unloaded, so I'm not sure if there any limits in what you can do, but worth a shot!

 

Adam

 

HI ADAM

 

thanks for reply. but i dont know anything about ajax.

 

vineet

Or if you don't want to have it use a database you could store it all in a multi-dimensional array in a session variable. That way when the browser is fully closed the cart will be destroyed. Saves making an extra table in your database.

Or if you don't want to have it use a database you could store it all in a multi-dimensional array in a session variable. That way when the browser is fully closed the cart will be destroyed. Saves making an extra table in your database.

 

hi projectfear

 

thanks for the reply. i got the idea

 

vineet

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.