Cyjm1120 Posted November 17, 2014 Share Posted November 17, 2014 Hi everyone, as the title stated, I would like to know how I could submit a form in one page(cart) and unset a session variable on other page. Right now my form is setting a variable within the page back to 0, however I would like the form to unset the session variable from other page as well. The issue with my code is that every time I attempt to clear out all items, my code can empty out the cart. But whenever I close and reopen the cart, the same content will show up again. So I need to unset the session variable. Please let me know what should I do in order to solve the problem and I greatly appreciate your help. My code is currently looking like this: cart.php if(!empty($_GET['aID'])) { $aID = $_GET['aID']; echo $aID; if(isset($_POST['removeAll'])) { $aID = "0"; } $cartSQL = "SELECT * from article where aID in ($aID)"; echo "cart sql :$cartSQL"; $cartQuery = mysqli_query($dbc, $cartSQL) or die (mysqli_error($dbc)); while($row = mysqli_fetch_array($cartQuery, MYSQLI_BOTH)) { $aTitle[] = $row[ 'name' ]; } } <form action = "" method = "POST"> <td style = "width = 200px"><input type="submit" value="Empty cart" name="removeAll"></td> </form> And this is the session variable from other page where I would like it to be unset upon submitting the form. if(!empty($cartSubmit)) $_SESSION["cartSubmit"] = $cartSubmit; else $cart = $_SESSION["cartSubmit"]; <a href="javascript:popup('cart.php?aID=<?php if(empty($cartSubmit)) echo "$cart"; else echo "$cartSubmit";?>')">Cart</a> Quote Link to comment https://forums.phpfreaks.com/topic/292524-submit-a-form-and-unset-a-session-variable-on-other-page/ Share on other sites More sharing options...
ginerjm Posted November 17, 2014 Share Posted November 17, 2014 Perhaps there is some confusion about session vars. A $_SESSION var is accessible from any script running within the same 'session', hence the term 'session var'. So if your script has set a session var, another script that runs after that and opens the same session will naturally have access to the same sesson vars and that script can then unset anything in it Quote Link to comment https://forums.phpfreaks.com/topic/292524-submit-a-form-and-unset-a-session-variable-on-other-page/#findComment-1496823 Share on other sites More sharing options...
Cyjm1120 Posted November 17, 2014 Author Share Posted November 17, 2014 Thanks for your prompt response. I just started learning php for a month and I am new to session var. I tried to add session_unset() into my script but it still wouldn't reset the session var saved on the other page. if(isset($_POST['removeAll'])) { $aID = "0"; session_unset(); } What changes to my code do you suggest so that I could unset the session var. Quote Link to comment https://forums.phpfreaks.com/topic/292524-submit-a-form-and-unset-a-session-variable-on-other-page/#findComment-1496828 Share on other sites More sharing options...
ginerjm Posted November 17, 2014 Share Posted November 17, 2014 If you have session var such as $_SESSION['cartsubmit'] then to remove it you say unset($_SESSION['cartsubmit']); session_unset would do something you dont' want to do. Quote Link to comment https://forums.phpfreaks.com/topic/292524-submit-a-form-and-unset-a-session-variable-on-other-page/#findComment-1496829 Share on other sites More sharing options...
Barand Posted November 17, 2014 Share Posted November 17, 2014 Before you can use session_unset() you need to call session_start(); All pages using $_SESSION need session_start() at the top of the script. Quote Link to comment https://forums.phpfreaks.com/topic/292524-submit-a-form-and-unset-a-session-variable-on-other-page/#findComment-1496836 Share on other sites More sharing options...
Cyjm1120 Posted November 17, 2014 Author Share Posted November 17, 2014 (edited) Thank you so much for your help guys. Edited November 17, 2014 by Cyjm1120 Quote Link to comment https://forums.phpfreaks.com/topic/292524-submit-a-form-and-unset-a-session-variable-on-other-page/#findComment-1496837 Share on other sites More sharing options...
Cyjm1120 Posted November 17, 2014 Author Share Posted November 17, 2014 Thank you so much for your help. I have one more question and hope you guy can help. I would like a page to reload/refresh every time a form is submitted, regardless of what page the form is submitted. My situation is that I have a add.php and cart.php and a main page. I would like when both of the add and cart php submit something, the main page will reload as well. Please let me know your thoughts and I appreciate your suggestion Quote Link to comment https://forums.phpfreaks.com/topic/292524-submit-a-form-and-unset-a-session-variable-on-other-page/#findComment-1496838 Share on other sites More sharing options...
CroNiX Posted November 17, 2014 Share Posted November 17, 2014 When the form is submitted and the data processed, just redirect back to the main page? Quote Link to comment https://forums.phpfreaks.com/topic/292524-submit-a-form-and-unset-a-session-variable-on-other-page/#findComment-1496840 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.