1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: window.history.go, PHP, and $_SESSION

Discussion in 'Web Design & Development' started by andynic, May 11, 2015.

Thread Status:
Not open for further replies.
Advertisement
  1. andynic

    andynic Thread Starter

    Joined:
    May 25, 2007
    Messages:
    385
    At http://www.w3schools.com/jsref/obj_history.asp is says that "go Loads a specific URL from the history list".

    I have a PHP script that, from an editing page, puts up a "preview" page. The "preview" page contains a "go back" button which returns the user to the editing page with the data as s/he has last left it.

    When the "preview" page's PHP script executes, it sets a session variable $_SESSION['preview'] = "TRUE";

    When the user clicks the "go back" button, at the top of the script that puts up the editing page, I have
    if ( isset($_SESSION['preview'] ) ) { echo "preview session var is set"; }

    However, even though the "preview" script has executed and sets the session var (verified by an echo in that script), with history.go(-1), the value is lost upon re-entry to the editing page.

    It seems that history.go(-1) not only goes back to the preceding URL, it also changes the session state in the sense described above (in other words it not only goes back to the preceding URL but also goes back to the previous session state).

    The editing page needs to know that the user is coming back from the preview page. Is there some way that I can achieve this without having to execute a submit from a form tag with method="post" or without opening the preview page in a different tab or window?

    Thanks for your help.
    Andynic
     
  2. andynic

    andynic Thread Starter

    Joined:
    May 25, 2007
    Messages:
    385
    I just realized that history.go does not interact with PHP at all, i.e. it does not re-execute the php script but it does cause the onload in the <body> tag to re-execute. And that's my problem!
     
  3. JiminSA

    JiminSA

    Joined:
    Dec 15, 2011
    Messages:
    3,386
    First Name:
    Jim
    Javascript is client side and php is server-side and never the twain shall meet - except via Ajax (Asynchronous JavaScript - not the Soccer Team:D)
    Take a look at this, one of many Ajax tuts, as I think it may help you to re-think your design;)
     
  4. andynic

    andynic Thread Starter

    Joined:
    May 25, 2007
    Messages:
    385
    Yes, I sometimes wonder where my head is.

    Thanks for the tip, Jim. I will look into that for future use. In the meantime I found a solution:

    The problem was that Firefox (the chosen standard for in-house admin scripts) was causing a resubmit with history.go(-1) causing the admin page to re-fetch the data from the DB, and hence re-execute the onload in the <body> tag. Consequently the user lost any changes when returning from the "preview" page.

    To solve the problem I resorted to a submit (method = 'post'), saving the $_POST array to $_SESSION['post_data'] upon entry to the preview page and then upon reentry to the admin page, bypassing the DB fetch and restoring instead the $_POST from the $_SESSION array.

    A simple histor.go(-1) would have been far simpler, but Firefox insists on the "resend".
    Andynic
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Similar Threads - Solved window history
  1. Gabarshek
    Replies:
    1
    Views:
    445
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1148045

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice