• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 379
  • Last Modified:

Passing variables to multiple pages

Hi,
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
0
82er
Asked:
82er
  • 2
1 Solution
 
matt_mcswainCommented:
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:

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

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

<?php
session_start();
echo $_SESSION['firstName'].' '.$_SESSION['lastName'].  ', Thank You for your purchase!';

?>
Hope that helps.
0
 
hujiCommented:
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:

<html>
<?php
session_start();
...

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
huji
0
 
robknowlesCommented:
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

config.php
<? this is some code

?>

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

<?
include "config.php";
session_start();
?>

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 :(

Cheers
0
 
hujiCommented:
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,
Huji
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now