tags:

views:

156

answers:

6

In one form, I ask a user for his name, I use POST and send the user to FormTwo.php and save the $_POST["name"] to the session.

In the second for I ask for his lastname and do the same and send him to the registerComplete.php page.

I'm trying to echo all the information he wrote, which by my understand should be accesible through the $_SESSION array, right?

What am I doing wrong? :)

RegisterFormOne.php:

<?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">
            <dt>First Name:</dt>
                <dd><input type="text" name="firstName" /></dd><br />

            <dt>Last Name:</dt>
                <dd><input type="text" name="lastName" /></dd><br />

            <dt>Age:</dt>
                <dd><input type="text" name="age" /></dd><br />

            <dt>Date of Birth:</dt>
                <dd><input type="text" name="dateOfBirth" /></dd><br />

            <dt>Gender:</dt>
                <dd>Masculino <input type="radio" value="M" name="gender" checked/> &nbsp;&nbsp;
                Femenino <input type="radio" value="F" name="gender" />
                </dd>
            <dt><input type="submit" /></dt>
        </form>
    </body>
</html>

RegisterFormTwo.php

<?php
if ($_POST){
  $_SESSION["firstName"] = $_POST["firstName"];
  $_SESSION["lastName"] = $_POST["lastName"];
  $_SESSION["age"] = $_POST["age"];
  $_SESSION["dateOfBirth"] = $_POST["dateOfBirth"];
  $_SESSION["gender"] = $_POST["gender"];
}
?>
<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>               

        <form action="registerFinish.php" method="post">
            <dt>Nationality:</dt>
                <dd><input type="text" name="nationality" /></dd><br />

            <dt>Profession:</dt>
                <dd>
                    <select name="profession">
                        <option value="sistemas" selected="selected">Ing. Sistemas</option>
                        <option value="electrico">Ing. Electrico</option>
                        <option value="marketing">Marketing y Publicidad</option>
                        <option value="comercio">Comercio</option>
                    </select>
                </dd><br />

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

registerFinish.php

<?php
if ($_POST){
  $_SESSION["nationality"] = $_POST["nationality"];
  $_SESSION["profession"] = $_POST["profession"];
}
?>
<html>
    <head>
        <title>Registration Complete</title>
    </head>

    <body>
        <h1>Thank you for taking the census.</h1>
        <p>Please take a moment to review your inputted information and confirm when you feel everthing is OK.</p>               

        <ul>
            <li><?php /*This should echo his first name, but nothing shows. */ echo $_SESSION["firstName"]; ?></li>

        </ul>

    </body>
</html>
+6  A: 

You miss session_start(); at the beginning of other 2 files.

Ondrej Slinták
Does using session_start() reset the saved information though?
Serg
No, it does not, it resumes the current one. From php.net "session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.".
middus
Nope, think of sessions as a global array that can be used when `session_start` is called
Anthony Forloney
Nope, session is saved as a special cookie on your hard disk and session_start(); just says you want to work with it.
Ondrej Slinták
+1  A: 

RegisterFormTwo.php and registerFinish.php need session_start(); at the start of the file.

David_001
+1  A: 

You must use session_start() before accession the $_SESSION variable ^^

Orestes C.A.
+2  A: 

The session must be started, using session_start(), for each execute of a script in which you want to use $_SESSION.

Here, you have 3 distinct scripts, that are executed via 3 distinct HTTP requests...
Which means session_start() should be called at the beginning of the 3 scripts -- and not only the first one.


As a reference, you can take a look at the Session Handling section of the manual.

Pascal MARTIN
A: 

In addition to your question, it would be easier if you do the following. Please note I intentionally put the form within an array within session because it's being automatically populated and a new form submission will flush the values (which you want).

$_SESSION['form'] = array();    // flushes all the stored form collection in session 
foreach ( $_POST as $key => $value )
{
    $_SESSION['form'][$key] = $value;
}

// now you can access your form's "name" field via $_SESSION['form']['name']
TravisO
A: 
session_start();

does not clear your session data. Session data will only be cleared when you

session_unset();
session_destroy(); 

or for individual session var

unset( $_SESSION[ 'yoursessionvar' ] );
David Morrow