clear shopping cart content on body unload or browser exit

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vinpkl
Forum Newbie
Posts: 9
Joined: Tue Aug 12, 2008 5:34 am

clear shopping cart content on body unload or browser exit

Post by vinpkl »

hi all

i am looking for a function that clears the contents of the shopping cart on closing the browser or browser exit 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 or browser exit.

this is my script

Code: Select all

 
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
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: clear shopping cart content on body unload or browser exit

Post by Jade »

You'll need to use javascript on body unload to call the URL of that file. That way every time the window is closed, your php script runs and clears everything out.
vinpkl
Forum Newbie
Posts: 9
Joined: Tue Aug 12, 2008 5:34 am

Re: clear shopping cart content on body unload or browser exit

Post by vinpkl »

Jade wrote:You'll need to use javascript on body unload to call the URL of that file. That way every time the window is closed, your php script runs and clears everything out.
hi jade

thanks for the reply. I got a example code from this site, can u tell me where to add the url of that file as this code will come in all my pages because user can exit from any page.
My php file name is shopping_cart.php

Code: Select all

 
<html>
<head>
<script language="JavaScript">
function redAlert() 
{
confirm("Are you sure you want to leave this Web page?");
}
</script>
</head>
<body onUnload="redAlert()">
</body>
</html>
 
vineet
Post Reply