Jump to content

submit a form and unset a session variable on other page


Cyjm1120

Recommended Posts

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>
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.