Make hierarchical back button in either javascript or php - php

I'm creating a mobile theme for my website and I decided it'd be nice to make a back button for people that shows up in the header of the site next to the title. Now I figured out how to make a back button in both javascript or php, and this works just fine in taking the user back to the last page visited.
Javascript:
<input type="button" value="Go Back From Whence You Came!" onclick="history.back(-1)" />
PHP:
<?php
$url = htmlspecialchars($_SERVER['HTTP_REFERER']);
echo "<a href='$url'>back</a>";
?>
Now the issue is I don't want people to go back to the last page they visited but rather go up the hierarchy of pages back up to the home page eventually.
Right now this is how it works:
I go to page A
Then I go to page B, and click to go to page C.
When I click the back button on page C, I get to page B.
When I click the back button on page B, I get to page C.
How I want it to work:
I go to page A
Then I go to page B, and click to go to page C.
When I click the back button on page C, I get to page B.
Then when I click the back button on page B, I get back to page A.
So I need it to work more like the normal back button on the browser and not just take you back to the last page you were on because that leads to an infinite loop which I'm sure will make people upset.
Also it's probably worth noting that I'm working on a huge site with tons of content built off of Drupal 6 on 1 page.tpl.php file. So I can't simply say if on page B go to page A but it needs to be something dynamic that works no matter where I plug it.
Thank you so much for your help!

If you still want to add this button (which might screw up the forward button for your browser) you can probably do it with a php session object or whatever it is called, I dunno php that well but here is the logic which I think will work
Each time you go to a page, save URl in this persistence variable/array, output the last entered URL as the url of the button then add current URL to this array. when you click the button you need to delete the URL in the link from the array and everything after it.
a-b-c-d
if you are on page d and press back, you need to remove c and d and output the link as b
OR
if you are on d and press back you need to remove d and output b as URL
then when are at the end meaning you are on page a, the button will disappear.
his seems like a lot of work to put in something that exists in every browser

Related

Show image on button pressed in another page php

I would like to show certain image on a page which is open in a tab, only when the other page press a button. I know I can post on the other page, but is it going to be live? Because I am not gonna be refreshing the page at all. My whole plan is to press a button on one page and show live result on an another page without refreshing it. How can I make it possible? I just need the basic idea behind it. Sorry if I made it sound complicated.
Thanks

why is browser the back button not directing to the last viewed page?

I'm working on a site (reworking is probably a better word as I did not build it originally) and am encountering the following weird scenario:
Users go to a page which shows a list of current events. This is called "whats-on".
I have added a link to this page which takes users to a full event calendar for the month in a typical calendar format. This page is called "event-calendar". Once on the page they can also select which month they want to see and what type of events they are interested in.
Users can click on an event listed in any date to navigate to a page with the event details. (page is event-details with a query that pulls the relevant event.) I have added a back button to the page under the event listing which will return them to the calendar showing the month and search results they just looked at. No issues there - I am using PHP $_SERVER['HTTP_REFERER'] and that works perfectly.
Here is the issue - if I just did a back() or history.go(-1) link on that button, it takes the user all the way back to "whats-on". The same thing happens when you click the browser back button. It just skips over "event-calendar" entirely.
This apparently happens on the site under other scenarios such as business listings and searches.
Obviously I do not want that behavior - I want the back button to go to the previously viewed page like it should!!
I cannot think of anything in the code that would cause this, but the original site developers included a whole bunch of JQuery packages such as jQuery UI, and I'm wondering if something could be interfering with the default back button behavior. Is that even possible??
I guess what I want to know is there is any JQuery code that would change the behavior of the back button so I could hunt it down and kill it!!
Just looking for ideas as to where to start looking.
Apparently the only way I can get it to work is to manipulate the browser history using
history.pushState({}, '', 'event-calendar?<plus whatever query was used to pull the data>');
on the calendar page itself.
This seems to work but what a stupid workaround. Wish I could find the original problem.

page navigation in html

Hi i am tryng to implement a web application and i was wondering if html offers any page navigation techniques.
My query is Am going like this in my current page navigation,
Page A --> Page B --> Page C --> Page B
if i press back from this Page B position it will go to Page C, I dont want that to happen, instead, i want it to go to Page A.
The reason is that consider
Page A as login page
Page B as home page with some options, when clicked on any one it will go to Page C
Page C corresponding clicked function, say text boxes where i can input name, address etc, and when i fill and click on submit, it will go to page B.
Now if i press on back button from this Page B, i will be navigated to Page C. I dont want this to happen, i just want to stay in that page in this case.
but if the scenario was like this
A->B->C->D->C..
then on pressing back from last C i need to go to B, and if i press back from B, either stay on that page or prompt me to logout.
Hope my question is clear please dont down vote this, i want to learn, only these forums are there to help me
Please help
The browser-back-button belong to the user.
Its a "go back in history"-button.
so ... imo dont touch it.
provide a useful navigation so that user dont even think about using the back-button =)
There are two ways to do this as far as i am aware.
Personaly i would use JS to create a simple logon page, that opens a page containing an iframe, which is linked to your site. If the user hits back, it will take them to the logon page, as the site is contained within an iframe.
The other way is to change the propertys of the back button, rather than removing it with JS. I think this can be done with.history, but i have never used personaly so i dont know.
Hope this helps :)

Back button on pages with tabs

I'm doing a system where the pages are displayed by tabs (easytabs Plugin http://os.alfajango.com/easytabs/#tabs1-js)
At one point in the process it performs a query in the database, where we
see all the details of each item that appears. I'd like to go into the details of the item, click back to the (on broswer or a button) the page to return to the list displayed by the SQL query, just as with any single page, but currently it only displays a blank page.
I wonder how can I do to implement this solution using the layout tabs.
Thank you.
There are various ways to maintain UI state. One of them is using a "url hash". For example, you click tab 2, change the url hash to:
mypage.php#tab=2
Then if someone presses BACK and goes FORWARD, you use JavaScript to look at the URL has and determine that the user had previously pressed tab 2, so you run your own bit of code that triggers the same event.
I'm unfamiliar with "easytabs" so I'm sorry I can't help with the specific implementation details, but that's the gist of the technique.

Make browser to go back by reloading page 1st and then scrolling it back again too

EXPLAINING WHAT I'M TRYING TO SOLVE:
I have a webpage (file_list.php) showing a list of files, and next to each file there is a button to delete it. When user press the DELETE button close to a certain file name, the browser goes to a script called delete_file.php that deletes the file and then it tells browser to go back to the file_list.php
delete_file.php uses a simple header("Location: file_list.php”); to go back to file_list.php
When browser goes back to file_list.php it reloads the page, but it DOES NOT scroll it back again to where the user was before. So let's say the user scrolled the files list and deleted the last file, when the browser shows again the page file_list.php it won't be scrolled to the bottom of the page again.
THE WORKAROUND I CAME OUT WITH:
I found a strange way to work around this, basically instead of using header("Location: file_list.php”); in delete_file.php I simply use a javascript call window.history.go(-1).
This workaround works perfectly when user is in session (simply using PHP session_start function): the browser RELOADS the file_list.php page and then scrolls it also back to where it was before.
But if the user is NOT in session the browser scrolls the page but IT DOES NOT RELOAD IT before, so the user would still see the file he deleted in the file list.
THE QUESTIONS
Do you know how to reproduce the behavior of the browser when goes back being in session even if we are not in session?
Do you know a way out of this, even another way of solving this matter?
Thanks!
I know I could use AJAX to delete the file so I would not have to go every time to delete_file.php, but this is not the answer.
You could emit anchors:
<a name="anchor1"/>filename_this
<a name="anchor2"/>filename_that
To delete filename_this, you pass the delete page filename_this and also anchor1. The delete page then redirects to file_list.php#anchor1
Note that the anchor names shouldn't map to the file names. That way when you delete the fifth file, the anchor is near the "new" fifth file (where the old one used to be).
Browsers generally remember where you were. If you go from page A to B, then go back (with the back button), you should arrive at the same place in page A.
When you use header('location: A.php');, you are instructing the browser to go forward to another page. It has never been there before, so it can't know what the scroll position was.
When you use history.go(-1), you are instructing the browser to click the back button, which is why your workaround works. You are likely emitting a no-cache header, which is why the browser is reloading the page. The effect here is the same as just reloading the page (without navigating). The trouble with this is that then the user can click the "forward" button, and arrive at your delete_file.php again (and may end up accidentally deleting another file).
Some ideas:
You can put a bunch of anchors on the page (one for each file), and redirect to the anchor that is closest to where the user clicked. So, if you deleted file 4, then redirect to file 3 header('file_list.php#file3');.
Compute the current scroll position in javascript, and store it in a cookie. When the page reloads, use javascript to scroll to where you were (making sure to only do this only once, it would be confusing to visit the page 3 days later and scroll to the middle for no particular reason).
You can also use AJAX to delete the file in the background. Here you are letting the browser deal with the scrolling. This might not be the answer you're looking for, but it's certainly a solution.
In my opinion, the best solution is the simplest: make your pages shorter. If your pages aren't long enough to scroll, then there's no problem.
How about submitting to an IFRAME? (no JS at all? you will not be able to hide the deleted entry...)
So to summarize, your steps are:
1) "a webpage (file_list.php) showing a list of files"
2) "delete_file.php that deletes the file and then it tells browser to go back to the file_list.php"
I'm assuming file_list.php only shows the current contents of the directory, which would obviously not include the file you just deleted. But you want file_list.php to somehow hint to the user something about the file they just deleted, like by jumping back to the position the file would normally be in the list had it still existed. There are probably several dozen ways to do this.
1) delete_file.php opens the url "/file_list.php#the_file_i_just_dele.ted". After you gather directory contents in an array, insert 'the_file_i_just_dele.ted' into that array, probably in alphabetical order or whatever. That array should have another boolean field: "deleted" => 1 | 0, which in case of the deleted file would be 1. While progressing through the array to echo the list, add a html anchor, which could simply be the filename, next to each list item. If 'deleted' is 1, don't echo the 'delete' button next to it. On page open, the page will jump straight to the 'the_file_i_just_dele.ted' anchor.
2) Use ajax.
3) Have a separate php library file for all your filesystem actions. file_list.php is the only page your client needs to see. On delete, file_list.php will post to itself with '#deleted' appended to the url, gather directory contents into an array, then delete the requested file, echo the array, and when your deleted file is encountered in the loop, disable or don't draw the 'delete' button and echo the 'deleted' html anchor right next to it. On page open, the client will automatically jump to the 'deleted' anchor.
4) Make a shopping cart and just not be concerned with this whole 'keeping track of where the user is on the previous page' problem. Have the user check off each file to be deleted. Use a cookie, DB, or session file to keep track of the cart contents if the "shopping" experience would span multiple directories and pages. User clicks a single 'delete' button at the bottom of the page. Add a 'checkout confirmation' page asking the user to confirm deletion of the listed files if you want to.
...etc...
You can also mix and match the solutions as you see fit.

Categories