php form - 2 buttons (one to destroy session and reload page and one to proceed to next page)
On the page 'selecteditems.php' I have a form like this:
<form method="POST" name="selecteditems" action="nextpage.php">
....i have some code here to display the values of the SESSION array in a table....
<input type="button" name="clear" value="Clear Session" onclick="window.location='selecteditems.php'">
<input type="submit" name="next" value="Go to Checkout">
</form>
Before the form on the 'selecteditems.php' page I have some code to add data ($_REQUEST params from the page that called 'selecteditems.php') to a $_SESSION array (this is working fine). Inside the form I have some code to display everything inside the $_SESSION array (this is working fine). If the session is empty it should print "session is empty".
My problem: I want to be able to click on the "Clear Session" button and have the session 开发者_如何学编程destroyed as well as the 'selecteditems.php' page reloading to say "session is empty" . If, the "Go to Checkout" button is clicked i would like to simply be sent to the nextpage.php page.
Any help would be appreciated in getting the 'selecteditems.php' to reload and echo "session is empty" after i have deleted the session.
Just put your clear session button as submit button with type="submit"
(you have it as type="button"
, which has inconsistent behaviour across browsers does nothing) and then you can just treat it as a normal submission process:
if(isset($_POST['clear'])) {
session_destroy(); // Or other session-unsetting logic
header("Location: selecteditems.php"); // Reload your page
}
if(isset($_POST['next'])) {
//next page logic
}
You might not even need to reload the clear session page. For the 'session cleared message' you could either add the logic to the $_POST['clear']
block, or redirect to 'selecteditems.php?msg=cleared' and search for a $_GET['msg']
and output the correct message, up to you :)
精彩评论