TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

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

5 点作者 jsteele超过 15 年前
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?

6 条评论

ionfish超过 15 年前
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.<p>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.
评论 #895935 未加载
frankus超过 15 年前
Often you'll have to explicitly call exit() after a call to header("Location: index.html?page=privacy").<p>IIRC it is version- and configuration-dependent.<p>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.
评论 #895928 未加载
Travis超过 15 年前
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.<p>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.
评论 #895938 未加载
1331超过 15 年前
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.
评论 #895948 未加载
byoung2超过 15 年前
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:<p><pre><code> index.php?page=contact -&#62; shows the form index.php?page=contact-submit -&#62; shows the confirmation with privacy policy text</code></pre>
评论 #895944 未加载
noodle超过 15 年前
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.<p>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.
评论 #895947 未加载