tags:

views:

40

answers:

1

I have two pages, A and B.

I want to echo a variable written in A inside of the page in B.

Here's the first page:

<?php session_start(); ?>

<html>
    <head>
        <title>Registration Form - 1 of 2</title>
    </head>

<body>
    <h1>Registration - Part 1 of 2</h1>
    <p>Please fill in all the required information before submitting the information.</p>
    <form action="registerFormTwo.php" method="post">
        First Name:<input type="text" name="firstName" /><br /><?php $_SESSION['firstName'] = firstName; ?>
        Last Name:<input type="text" name="lastName" /><br /><?php $_SESSION['lastName'] = lastName; ?>
        Age:<input type="text" name="age" /><br /><?php $_SESSION['age'] = age; ?>
        Date of Birth:<input type="text" name="dateOfBirth" /><br /><?php $_SESSION['dateOfBirth'] = dateOfBirth; ?>

        <input type="submit" />
    </form>
</body>

And here's the second one:

<?php session_start(); ?>

<html>
    <head>
        <title>Registration Form - 2 of 2</title>
    </head>

<body>
    <h1>Registration - Part 2 of 2</h1>
    <p>Please fill in all the required information before submitting the information.</p>        
    <?php //wrote this in just to test that session information is saving, but it isn't.
    echo $_SESSION['name']; ?>

    <form action="registerFinish.php" method="post">
        Nationality<input type="text" name="nationality" /><br /><?php $_SESSION['nationality'] = nationality; ?>
        Profession:<input type="text" name="profession" /><br /><?php $_SESSION['profession'] = profession; ?>

        <input type="submit" />
    </form>
</body>

On the second page, the name variable should be echoed, but nothing shows.

Thank you for the help.

EDIT:

Here's the code on formOne and it's still not working:

<?php session_start();
if ($_POST) {
  // Store our name in the session array
  $_SESSION["firstName"] = $_POST["firstName"];
}
?>

<html>
    <head>
        <title>Registration Form - 1 of 2</title>
    </head>

    <body>
        <h1>Registration - Part 1 of 2</h1>
        <p>Please fill in all the required information before submitting the information.</p>
        <form action="registerFormTwo.php" method="post">
            First Name:<input type="text" name="firstName" /><br />
            Last Name:<input type="text" name="lastName" /><br /><?php $_SESSION['lastName'] = lastName; ?>
            Age:<input type="text" name="age" /><br /><?php $_SESSION['age'] = age; ?>
            Date of Birth:<input type="text" name="dateOfBirth" /><br /><?php $_SESSION['dateOfBirth'] = dateOfBirth; ?>

            <input type="submit" />
        </form>
    </body>
</html>
+2  A: 

Form data doesn't go directly into $_SESSION. You have to place it there. Since your form method is POST, you could pull your data out of $_POST on the server-side:

session_start();
if ($_POST) {
  // Store our name in the session array
  $_SESSION["name"] = $_POST["name"];
}

If you simply want to keep values in a form when the form submission failed, you don't need to use sessions. You can reprint the value from $_POST directly into the markup again:

<input type="text" name="name" value="<?php print $_POST["name"]; ?>" />

Keep in mind that all of this takes place on the page that you're posting to. If your first form is on page1.php, then you would ignore all of this. If you're posting from page1.php to page2.php, you would place the aforementioned code on page2.php.

Update

I just noticed the following beside your form elements:

<?php $_SESSION['lastName'] = lastName; ?>

lastName doesn't represent anything here. $_POST["lastName"] would represent the posted data. If you were trying to print the last-submitted value, I would do this:

<?php print $_POST["lastName"]; ?>
Jonathan Sampson
It's still not working, can you please check my edit?
Serg