Reputation: 1744
I've got a php database driven website and I want to implement a feature where there is a section on the navigation bar which displays a history of previous pages visited (with their urls) on the website. I plan on using a $_SESSION
array to store this list.
That should be easy enough, but I also want to have a delete button next to every history entry so the user can remove pages from the history bar. The problem with this is that to update the $_SESSION
array and remove the entry, it will need to reload the page, and if the user is on a page such as a search results page it will come up with errors because the page generated relies on a submitted form. There would be other examples of this issue on other pages.
I'd rather not have to store the search etc. in a session variable to get around this so was wondering if there would be some way of using javascript to delete the entry on the history bar and then somehow update the php session variable from the javascript when the user navigates away from or refreshes the page... but I'm not sure how the js could change the php session variable. Could this work or is there another solution?
Upvotes: 0
Views: 246
Reputation: 7065
You can follow below steps:
Make an ajax call to a PHP
file and destroy SESSION
variables related to user's page visited history.
On ajax
success, hide / remove HTML
elements or container that display page history URLs on the web page using JavaScript
.
Since your search page is dependent on POST
values, this will ensure that user's page history is cleared without the need to refresh the page. When user navigates to some other link, page will get reloaded and user will not get older links.
Hope this helps!
Upvotes: 2
Reputation: 587
You can use AJAX, for example if you push logout button ajax will send information to the server side but php will do all the work of destroying that session. The are many examples of ajax on stackoverflow.
Upvotes: 1