0

I have just started using session variables and I'm running into some problems.

I'm trying to echo out a variable from my form processing script back onto another page but currently I'm not getting any results.

Before starting let me say that I have already checked out other questions including this one:

Session variables not working php

I checked everything off that first answer list and still nothing.
The only error i am getting is:

Notice: Undefined index: id

Which I'm assuming is because when the page first starts it doesn't know what id is but even after submitting the error persists.

index.php

<?php
    session_start();
    error_reporting(E_ALL);
    ini_set("display_errors", 1);
?>
<!DOCTYPE html>
<html>
<head>
    <title>PHP Sessions</title>
</head>
<body>
    <?php echo $_SESSION['id']; ?>

    <form action="submit.php" method="post">
        <input type="text" name="number" id="number">
        <button type="submit">Submit</button>
    </form>
</body>
</html>

submit.php

<?php
    session_start();

    try {
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $id = $_POST['number'];
        };

        header('Location: http://home.com/');
        exit();
    } catch (Exception $e) {
        echo $e->getMessage();
    }
?>
10
  • 3
    the notice is obvious, isn't it? you didn't assign anything to it. if(isset($_SESSION['id'])) { echo "It's set"; } else{ echo "Sorry"; } or if(isset($_SESSION['id'])) { echo "It's set"; } else{ $_SESSION['id'] = "ID123"; } Commented May 23, 2015 at 15:39
  • I'm a bit confused by your comment. $id is supposed to equal whatever the user inputs into the box and then when they hit submit it redirects back to the same page with the form and displays what they input. Commented May 23, 2015 at 15:44
  • then you need to assign the session array to the post array Commented May 23, 2015 at 15:44
  • 1
    so is it supposed to be $_SESSION['id'] = $_POST['number'] in the submit.php file? Commented May 23, 2015 at 15:45
  • 1
    you got it Pontiac ;-) Commented May 23, 2015 at 15:53

2 Answers 2

1

As mentioned in my comment, you didn't assign anything to it, which is why you're getting that notice. Therefore, use a conditional statement, using isset().

if(isset($_SESSION['id'])) { 
   echo "The session is set."; 
} else{ 
   echo "Sorry, it's not set."; 
} 

or

if(isset($_SESSION['id'])) { 
   echo "The session is set.";
} 
   else{ 
    $_SESSION['id'] = "ID123"; 
}
  • Pass your session array to a POST array:

From your initial page, you can use a ternary operator (see reference note) to echo if it's set or not.

<?php
    session_start();
    error_reporting(E_ALL);
    ini_set("display_errors", 1);
?>
<!DOCTYPE html>
<html>
<head>
    <title>PHP Sessions</title>
</head>
<body>
    <?php echo isset($_SESSION["id"]) ? $_SESSION["id"] : '' ?>

    <form action="submit.php" method="post">
        <input type="text" name="number" id="number">
        <button type="submit">Submit</button>
    </form>
</body>
</html>
  • Then in your "submit.php" page, passing the session array to the POST array:

Sidenote: I commented these lines out, and when putting those back in, comment echo $id;; otherwise it will thrown a notice.

// header('Location: http://home.com/');
// exit();

submit.php and using a conditional !empty() for the POST array.

<?php
    session_start();

    try {
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {


    if(!empty($_POST['number'])){

       $_SESSION['id'] = $_POST['number'];

    // $id = $_POST['number'];

       $id = $_SESSION['id'];

        echo $id;

    }

        };

// header('Location: http://home.com/');
// exit();
    } catch (Exception $e) {
        echo $e->getMessage();
    }
?>

The '' at the end of isset($_SESSION["id"]) ? $_SESSION["id"] : '' can be used to put a default text/number if you wish.

I.e.:

isset($_SESSION["id"]) ? $_SESSION["id"] : 'default value'

Reference(s):

Sign up to request clarification or add additional context in comments.

Comments

0

The $_SESSION variables, like all variables, have to be created before. On your form you try to display a variable but it doesn't exists yet.

If you try to initialize the session just before, it works.

//If the variable exists, display it.
if ( isset($_SESSION['id'] )
    echo $_SESSION['id'];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.