Passing variables to multiple pages

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
Who is Participating?
matt_mcswainConnect With a Mentor Commented:
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.
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
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 :(

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,
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.