

How best to navigate to a PHP page after a form submit? - jsteele

Experienced PHP programmers know that there are various ways of structuring pages within a website.  Years ago, a common approach was to place every major page into a separate PHP file, and within the navigation menus simply link directly to the various pages.  Nowadays, most sites use a central control file -- typically index.php -- and use GET variables to distinguish which page within the system the control file should present to the visitor.  (The control file might pull the page content from functions, HTML files, or, as is used in the better CMSs, a database.)<p>For instance, a "Contact" navigation menu item could be linked to "index.php?page=contact".  (For this discussion, let's ignore search-engine-friendly URLs.)<p>What is the best way to pass the visitor from a completed form page to a destination page?  Let's say that after visitors to our site successfully complete the contact form, we want to send them to our privacy policy page.  So when they are completing the form, the address in the browser is "index.php?page=contact".  After they click the Submit button, we process the input, verify that it is all correct, and now want to send them to the privacy page, whose link in the navigation menu is "index.php?page=privacy".  How best to do that?<p>One possible approach is to use the PHP function header().  But if you are stepping through the contact page code, using a PHP IDE, it will step to the line of code after the header() call, without navigating to the privacy page.  (There may be an IDE out there that does what we want, but I have yet to find one.  Even if it did, all variables set in the contact page code are lost.)<p>A second possible approach is to have the form processing code set a variable to indicate what page to go to next (e.g., $page = 'privacy'), which would cause the privacy page's contents to be displayed next.  But the address in the browser still indicates that the visitor is on the contact page.  They may send that address to someone else, as a link to the privacy page, which would be confusing to the recipient.<p>Any suggestions?
======
ionfish
Well, to start with, you shouldn't be letting your choice of IDE dictate your
code architecture. IDEs are a developmental aid (some might say a
developmental crutch), so if yours is getting in your way, it's not doing its
job and you should reconsider your use of it.

When you talk about using "the PHP function header()", I presume that you
intend to use it to issue an HTTP redirect response. Think about what you're
doing: HTTP is stateless, but you want to preserve state across page requests
(the privacy page is a different resource to the contact form, as your
uneasiness about not changing URIs shows you're aware). The place to do this,
generally speaking, is in the session.

~~~
jsteele
It's not dictating the code architecture -- it's one of many factors to
consider

Naturally I'm using a session variable. But the URL mismatch remains.

------
frankus
Often you'll have to explicitly call exit() after a call to header("Location:
index.html?page=privacy").

IIRC it is version- and configuration-dependent.

But a redirect is pretty clearly the right way to do this. When you get ready
to do SEO-friendly URLs, take a look at mod_rewrite.

~~~
jsteele
Thanks for the reply. How does an exit() after header() help? It still causes
a discontinuity in an IDE.

------
Travis
I also use the header() call to send an HTTP response code. Most of the time,
I want my users to be able to retrieve the information on that page later (say
it's an order number / summary / etc.) Lots of people will copy the URL in
their browser bar; if you do a server side alteration, at the same URI, for
different content, it cannot be distinguished.

OTOH, sometimes you just want a "thank you" page, with no info on it. And you
want people returning to the site to see the form, rather than the thank-you.
Then, IMO, it's appropriate to do a server side include of the content in
question.

~~~
jsteele
I agree with all that, but how do you solve the problem I posed initially?

------
1331
Your question is a common one in web development. I highly recommend learning
how to use Zend Framework. Even if you choose to not use the framework for any
real projects, the things that you learn will significantly improve your
knowledge of PHP and give you a much better understanding of how to deal with
problems such as the one posed by your question.

~~~
jsteele
I tried ZF a while back, but don't recall how it would resolve this issue. Can
you be specific?

------
byoung2
After the user submits a form, it is usually a good idea to display a
confirmation page. It might be jarring to go directly to the privacy page. I
would set it up like this:

    
    
      index.php?page=contact -> shows the form
      index.php?page=contact-submit -> shows the confirmation with privacy policy text

~~~
jsteele
The privacy page is just an example. Your scenario replaces it with a
confirmation page. So the problem still stands: How do you go to the
?page=contact-submit page? Do you use header()? Is that the canonical way?

------
noodle
like you said, there's limited options, and each one has its individual
drawbacks, but header redirect fits my needs when i'm doing php development.
ymmv.

plus, when making decisions, i tend to follow the "whats best for my users"
test. in this case, as you've stated it, its seamless IDE integration vs
having a consistent external link structure. a no brainer, imo.

~~~
jsteele
All true. I've bounced back and forth. On a previous project, there was no
need for stepping through the code, so header() trumped. My current project
has benefitted greatly from the IDE, so I'm using a session var (and hoping
the user refrains from bookmarking! :)

