Passing variables to multiple pages

Posted on 2005-04-05
Last Modified: 2013-12-13
I have trouble figuring out the best way to send variable values to more than one page. For example, there's a form to fill out. I want the confirmation page to display the info filled in the form and also the processing page to add those variable values to the database. Anybody can give me a hint?
Thank you very much
Question by:82er
    LVL 11

    Accepted Solution

    I would make the form page post to itself and process the form there. Then upon successesful validation and update of the database redirect to your confirmation page. To display the confirmation page, you could just pull the information from the database based upon an order id, for example, but it'd be better you have all this information in session variables as well.
    So as soon as the user starts entering information, no matter how many steps, update their session with the appropriate information so it's readily available from one page to the next. So for example:

      if (isset($_POST['submit'])) { //upon submission
        //update database
       $_SESSION['firstName'] = $_POST['firstName'];
       $_SESSION['lastName'] = $_POST['lastName'];
      header('Location: confirmation.php');

    Then on your confirmation page, just resume the session and all your data is there.

    echo $_SESSION['firstName'].' '.$_SESSION['lastName'].  ', Thank You for your purchase!';

    Hope that helps.
    LVL 14

    Expert Comment

    Don't forget that:
    1) You can use $_SESSION variables on third, .... pages too, if you all session_start() in them.
    2) Session_start() function needs to be placed on the first line of your PHP code.
    3) You must have NO OUTPUT before the PHP line session_start(); For example this will make an error:


    Because there as <html> before the session_start() is called.
    4) Session variables consume your server's memory so use them wisely.
    Wish I can help
    LVL 1

    Expert Comment

    A small thing to note about sessions, which quite often catches me out, is that if your using include statements before you do a session_start then you must ensure that the files included don't give any output to the browser (as mentioned above), otherwise you'll get an error, which also means that you need to keep an eye out for blank lines (e.g. crlf combinations).

    for example

    <? this is some code


    -- file ends at this line, with a blank line after ?>

    include "config.php";

    This will give an error, as the blank line at the end of config.php is sent as output to the browser.

    Just something to keep in mind, and gets very annoying if you use a web based editor like me, which has a tendancy to add blank lines at the end of files :(

    LVL 14

    Expert Comment

    I wonder if matt's answer really worths as little 50 points with a B. It was a good an complete answer, directly focusing on your question. I recommend you to read the grading tips again, since I see you are not an old member here.
    With regards,

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    A colleague recently asked me about how to give his client a small part of the web site that could be completely under the client's control.  Since I have done this sort of thing before to add emergency banners to a web site, I decided I would creat…
    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now