0

I am working with sessions When I submit my form from page 1 to page 2, on page 2 I keep getting the notice of undefined variable. Do you all see something I don't ? Also I am only putting pertinent HTML. Thank you!

P.S. - (I read the similar posts but they do not answer my question they only mention checking the assignment operators. Also, I looked at the example that says “already answered here” but it does not help me because in that stackoverflow post it is assuming I know what the username is. In my case it is different because the username is whatever the user enters, it’s not predetermined by me! )

PAGE 1: HTML/PHP

<form action="L10_Combo2.php" method="POST">
        <p>Please enter your username</br>
        <input type="text" id="username" name="username"/>        
        </p>
        <p>Please enter your email address</br> 
        <input type="email" id="email" name="email"
        </p>
        </br></br>
        <input type="submit" name="submit" value="Submit Information"/>
        </form>

        <?php
              session_start();

        if(isset($_POST['submit'])){
            $username = $_SESSION['username'];
            $email = $_SESSION['email'];
        }
    ?>    

PAGE 2: L10_Combo2

  <?php

session_start();
 if(isset($_SESSION['username']) && isset($_SESSION['email'])){
                $username = $_SESSION['username'];
                $email = $_SESSION['email'];
            }

//This is where I am getting the undefined variable error!
print("Stored in the session: " . $username .  $email); 

session_destroy();
CodeConnoisseur
  • 1,452
  • 4
  • 21
  • 49
  • 3
    session should always start on top of the file. – urfusion Aug 16 '18 at 13:15
  • 2
    You'll never hit your IF statement on page 1. Your form submits to page2, so that's where you want to store your session. All PHP processing is done before the file is submitted to the browser, and does not capture anything after. – aynber Aug 16 '18 at 13:16
  • 1
    There's a point in your code where `$username` and `$email` can be completely undefined. Either declare the two at the top of the page with an empty value or just put an `else` on that `if` – IsThisJavascript Aug 16 '18 at 13:17
  • 1
    Apparently your if block parses to false and isn't executed in L10_Combo2 - check your session content using `
    =print_r($_SESSION)?>
    ` to see if you correctly started the section. (`session_start();` Should be placed on top of the file!) Plus your first page contains invalid code. You check for not existant POST data thus never write to the session. Also do you set a session name in either of the PHP files?
    – Chris S. Aug 16 '18 at 13:17
  • where have you set your session... set your session data and then try to check it.. – Avi Aug 16 '18 at 13:17
  • 1
    `isset($_POST['submit'])` has no effect since you are posting to L10_Combo2.php. You are not writing data to the session, why do you expect you can retrieve it? – Alien426 Aug 16 '18 at 13:18

1 Answers1

-1

When you are registering sessions, it needs to go to the form action page. Looks like your form action is on page #2

<?php
        session_start();
        if(isset($_POST['submit'])){
            $username = $_SESSION['username'];
            $email = $_SESSION['email'];
        }
    ?>    //Move this to page #2

Move your php script to the top on page #1

<?php
session_start(); //this needs to be on the top most code

if(isset($_POST['submit'])){ //this needs to move to the form action page, looks like its on page #2.
    $username = $_SESSION['username'];
    $email = $_SESSION['email'];
}

?>

Your html comes after the top snippet of the php code

<form action="L10_Combo2.php" method="POST">
        <p>Please enter your username</br>
        <input type="text" id="username" name="username"/>        
        </p>
        <p>Please enter your email address</br> 
        <input type="email" id="email" name="email"
        </p>
        </br></br>
        <input type="submit" name="submit" value="Submit Information"/>
        </form>



<?php
session_start(); //this needs to be on the top most code on page 2

if(isset($_SESSION['username']) && isset($_SESSION['email'])){
    $username = $_SESSION['username'];
    $email = $_SESSION['email'];
}

    //This is where I am getting the undefined variable error!
    print("Stored in the session: " . $username .  $email); 
    print_r($_SESSION);

    session_destroy();

Also do a

 print_r($_SESSION); to see what's in the sessions before session destroy to check what is actually in the session var. 
unixmiah
  • 3,081
  • 1
  • 12
  • 26