6 Replies Latest reply on Sep 11, 2007 3:34 PM by Newsgroup_User

    Remembering Last Page

    AngryCloud Level 1
      I have some links on my site that are direct links to PHP scripts with no output. How do I get these scripts to take the user back to the page they were on when they clicked the link?
        • 1. Re: Remembering Last Page
          Level 7
          AngryCloud wrote:
          > I have some links on my site that are direct links to PHP scripts with no output.

          The obvious question is: why? If a page produces no output, you
          shouldn't be linking to it.

          > How do I get these scripts to take the user back to the page they were on when they clicked the link?

          I assume that you want the user to activate a script that updates a
          database or does something else. The way to redirect anyone after a
          script has executed is to use header():

          http://www.php.net/manual/en/function.header.php

          --
          David Powers, Adobe Community Expert
          Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
          Author, "PHP Solutions" (friends of ED)
          http://foundationphp.com/
          • 2. Re: Remembering Last Page
            AngryCloud Level 1
            Yes, the scripts with no output are for updating databases (i.e. voting, deleting messages, posting a comment). I use the header function now to redirect the user back to the most likely page they would have been on. But some scripts are linked from more than one page.

            For example, the user's most recent journal entry is displayed on their page (user.php). Then there is the actual journal entries page (journal.php), which displays all of their journal entries. Beside the entries on both pages is a link to delete the entry, which is a link directly to a script with no output (delete_journal_entry.php). At the end of the delete script I use header to take the user back to the journal entries page, even if they deleted their most recent entry directly from the user page.
            • 3. Re: Remembering Last Page
              L._Simon Level 1
              you could use javascript -

              history.back(x), where x is the number of steps to go back. Also note, this leaves the forward history in the browser, so they can go back to the page via the forward command.
              • 4. Re: Remembering Last Page
                AngryCloud Level 1
                Well how do people generally get around this?

                David, you asked me why I would have links to pages with no output. Is this wrong or uncommon to do? If so, how else should I go about executing scripts that don't need any output?

                Like when a user deletes a journal entry, I would find it very unnecessary and somewhat inconvenient to have the 'delete entry' script output to the user that their entry was deleted. Instead I would just like to use header to take them back to the page from where they clicked the link to delete the entry, where they could plainly see that the entry was in fact deleted.
                • 5. Re: Remembering Last Page
                  bregent Most Valuable Participant
                  >?I have some links on my site that are direct links to PHP scripts with no output. How do I get these scripts to
                  >take the user back to the page they were on when they clicked the link?

                  There are a few options I can think of:

                  1) Have the processing page simply call a redirect back to the page that called it. You could either use a hardcoded url, or if it's a generic processing page, pass the calling url as a parameter in the querystring.
                  2) Use AJAX
                  3) Combine the two pages into one

                  I'm not really familiar with php so some other options may be available, like possibly other AJAX type asynchronous calls.
                  • 6. Re: Remembering Last Page
                    Level 7
                    AngryCloud wrote:
                    > Like when a user deletes a journal entry, I would find it very unnecessary and
                    > somewhat inconvenient to have the 'delete entry' script output to the user that
                    > their entry was deleted. Instead I would just like to use header to take them
                    > back to the page from where they clicked the link to delete the entry, where
                    > they could plainly see that the entry was in fact deleted.

                    When deleting a database record, it's sensible to display a page asking
                    the user to confirm that the correct record has been selected, and that
                    the record should, in fact, be deleted. When the confirm button is
                    clicked, send the user back to the original page.

                    One way of redirecting a user back to the original page is to use
                    $_SERVER['HTTP_REFERER']. Unfortunately, not all browsers support this,
                    so it can't be relied on 100%. An alternative is to append the name of
                    the referring page to the link's query string. You can then use the
                    $_GET array to retrieve the name of the page, and pass it to header().

                    --
                    David Powers, Adobe Community Expert
                    Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
                    Author, "PHP Solutions" (friends of ED)
                    http://foundationphp.com/