vinpkl Posted November 4, 2008 Share Posted November 4, 2008 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 Quote Link to comment Share on other sites More sharing options...
priti Posted November 4, 2008 Share Posted November 4, 2008 There is no way to tell you that user is going to close browser.What generally you can do is when the task is completed you can destroy the session. session_destroy or unset($_SESSION) hope it helps Quote Link to comment Share on other sites More sharing options...
Adam Posted November 4, 2008 Share Posted November 4, 2008 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 Quote Link to comment Share on other sites More sharing options...
steveclondon Posted November 4, 2008 Share Posted November 4, 2008 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. Quote Link to comment Share on other sites More sharing options...
vinpkl Posted November 4, 2008 Author Share Posted November 4, 2008 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 Quote Link to comment Share on other sites More sharing options...
Adam Posted November 4, 2008 Share Posted November 4, 2008 No worries. To be honest I'd go with steve's idea anyway.. Adam Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted November 4, 2008 Share Posted November 4, 2008 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. Quote Link to comment Share on other sites More sharing options...
vinpkl Posted November 4, 2008 Author Share Posted November 4, 2008 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.