Jump to content

Session variables not carried onto subsequent pages


babymac
Go to solution Solved by Jessica,

Recommended Posts

Hi everyone,

 

I'm new to PHP and am taking a class but things aren't coming together as I'd hoped.  I'm trying to utilize sessions to carry info from page to page.  It brings it to the next page, but not the next. 

What I'm trying to do is:

-Make a simple input form. 

-Show entered information on next page via the use of a session.

-Third page shows final information. 

 

On a bigger scale, I'd like to have some if statements added if a field is left blank but want to get this figured out first.  Thanks for any comments/suggestions!

 

-Lórien

 

FIRST PAGE:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://wwww.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <title> Input Form
    </title>
</head>

<body>
    
    <form action="pa2.php" method="POST">
        
        <p>First Name: <input type='text' name='fname' size='30'/>
        </p>
        <p>Last Name: <input type='text' name='lname' size='30'/>
        </p>
        <p>Email Address: <input type='text' name='email' size='45'/>
        </p>
        <input type="submit" name='enter' value="Submit" /><br/>
    </form>
    
</body>
</html>

 

 

SECOND PAGE:

 

<?php
    //Create a script that starts a session:
    session_start();
    
    $_SESSION['fname'] = $_POST['fname'];
    $_SESSION['lname'] = $_POST['lname'];
    $_SESSION['email'] = $_POST['email'];
    
    
    // Print the received data:
    echo
    '<p> You entered your first name as: ' . $_SESSION['fname'] . '
    </p>
    <p> You entered your last name as: ' . $_SESSION['lname'] . '
    </p>
    <p> You entered your email as: ' . $_SESSION['email'] . '
    </p>';


    ?>

    <a href="http://studentweb.wilmu.edu/sites/lpeer79627/pa2.html"><br />Click to go back and change your information</a>
    <a href="http://studentweb.wilmu.edu/sites/lpeer79627/pa2final.php"><br />Yes, my information is correct!</a>
 

 

 

THIRD PAGE:

 

<?php
    //Create a script that starts a session:
    session_start();
    
    $_SESSION['fname'] = $_POST['fname'];
    $_SESSION['lname'] = $_POST['lname'];
    $_SESSION['email'] = $_POST['email'];
    
    
    // Print the received data:
    print
    '<p><h2>Here is your information:</h2></p>
        <p> Name: ' . $_SESSION['fname'] . ', ' . $_SESSION['lname'] . '
    </p>
    <p> Email: ' . $_SESSION['email'] . '
    </p>';
        
?>    
 

 

 

Link to comment
Share on other sites

Yes!  I just figured it out.  Thank you so so much!  For future views to this post, I needed to delete the session variables at the top of the third page and just echo in the statements.  I do have one other question - I have updated the code a bit and would like to have one link show to go back and edit info, and another link show to continue to final screen.  They both appear at the bottom, and I don't know how to include them inside the php code.  Thoughts?

 

<?php
    
    //Create a script that starts a session:
    session_start();
    
    $_SESSION['fname'] = $_POST['fname']; // First Name
    $_SESSION['lname'] = $_POST['lname']; // Last Name
    $_SESSION['email'] = $_POST['email']; // Email Address
    
    // Check if the form has been submitted:
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    // Handle the form:
    if ( (empty($_POST['fname'])) or (empty($_POST['lname'])) or (empty($_POST['email'])) )
    
    // Forgot a field:
    echo '<p>Please make sure all input fields are entered.</p>';
    }
    
    
    if ( (!empty($_POST['fname'])) && (!empty($_POST['lname'])) && (!empty($_POST['email'])) ) {
    
    // Print the received data:
    echo
    '<p> You entered your first name as: ' . $_SESSION['fname'] . '
    </p>
    <p> You entered your last name as: ' . $_SESSION['lname'] . '
    </p>
    <p> You entered your email as: ' . $_SESSION['email'] . '
    </p>';
    }
    
?>

    <a href="http://studentweb.wilmu.edu/sites/lpeer79627/pa2.html"><br />Click to go back and change your information</a>
    <a href="http://studentweb.wilmu.edu/sites/lpeer79627/pa2final.php"><br />Continue to final screen</a>

Link to comment
Share on other sites

Sorry - I guess I'm not sure.  I am trying to put the href's at the bottom of the page up into the if statements.  One to go back if fields are not entered, and one to move forward to the next page.  I've tried wrapping them in '<p></p>' statments, etc, and nothing seems to take away the parse error.

Link to comment
Share on other sites

I actually have one other question if anyone could explain.  The code is doing what I want now, I just don't understand one part of the display.  When a field is not entered, the user is shown the statment "Please make sure all input fields are entered." and then the link to go back.  When all fields are entered correctly, the user is shown their information and a link to go forward as well as the link to go back.  The 'go back' link appears at the top.  While I'm happy it's there, it's not included in the if statement for when all fields are not empty.  Can someone explain to me why this is?

 

 

<?php
    
    //Create a script that starts a session:
    session_start();
    
    $_SESSION['fname'] = $_POST['fname']; // First Name
    $_SESSION['lname'] = $_POST['lname']; // Last Name
    $_SESSION['email'] = $_POST['email']; // Email Address
    
    // Check if the form has been submitted:
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    // Handle the form:
    if ( (empty($_POST['fname'])) or (empty($_POST['lname'])) or (empty($_POST['email'])) )
    
    // Forgot a field:
    echo '<p>Please make sure all input fields are entered.</p>';
    echo '<a href="http://studentweb.wilmu.edu/sites/lpeer79627/pa2.html"><br />Click to go back and change your information</a>';
    }
    
    
    

    
    if ( (!empty($_POST['fname'])) && (!empty($_POST['lname'])) && (!empty($_POST['email'])) ) {
    
    // Print the received data:
    echo
    '<p> You entered your first name as: ' . $_SESSION['fname'] . '
    </p>
    <p> You entered your last name as: ' . $_SESSION['lname'] . '
    </p>
    <p> You entered your email as: ' . $_SESSION['email'] . '
    </p>';
    echo '<a href="http://studentweb.wilmu.edu/sites/lpeer79627/pa2final.php"><br />Continue to final screen</a>';
    }
    
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.